11###
22# @meta Range
3- # @brief
4- # @details
3+ # @brief Create a ranged closure in interval [a, b[
4+ # @details Has a field `asList` to compute a list from the current state of the range, and another one `reset`.
55# ---
6+ # (let obj (range 1 10))
7+ # (print (obj.asList)) # [1 2 3 4 5 6 7 8 9]
8+ # (while (not (nil? (obj)))
9+ # (print obj.i)) # print the current element
10+ # (print (obj.asList)) # [], the range has been used
11+ # (obj.reset) # the range is ready to be used again
12+ # (print (obj.asList)) # [1 2 3 4 5 6 7 8 9]
613# ---
7- # @param
14+ # @param i the beginning of the range
15+ # @param _b the end of the range
816# @author https://github.com/SuperFola
917###
10- (let range (fun (_a _b) {
18+ (let range (fun (i _b) {
1119 (let asList (fun () {
12- # _a and _b are going to be captured by the caller
20+ # i and _b are going to be captured by the caller
1321 (mut _output [])
14- (mut a_ _a )
22+ (mut a_ i )
1523 (while (< a_ _b) {
1624 (set _output (append _output a_))
1725 (set a_ (+ 1 a_))
1826 })
1927 _output
2028 }))
2129
22- (fun (&_a &_b &asList) {
23- (if (< _a _b)
30+ (let _a i)
31+ (let reset (fun () (set i _a)))
32+
33+ (fun (&i &_a &_b &asList &reset) {
34+ (if (< i _b)
2435 {
25- (set _a (+ _a 1))
26- (- _a 1)
36+ (set i (+ i 1))
37+ (- i 1)
2738 })
2839 })
2940}))
3041
3142###
3243# @meta Range
33- # @brief
34- # @details
44+ # @brief Run a function on each element of the range
45+ # @details The range is unmodified.
3546# ---
47+ # (let obj (range 1 10))
48+ # (range:forEach obj (fun (e) (print e)))
3649# ---
37- # @param
50+ # @param _r the range object
51+ # @param _f the function
3852# @author https://github.com/SuperFola
3953###
4054(let range:forEach (fun (_r _f) {
4155 (mut _val (_r))
42- (while (not (nil?_ val )) {
56+ (while (not (nil? _val )) {
4357 (_f _val)
4458 (set _val (_r))
4559 })
60+ (_r.reset)
4661}))
4762
4863###
4964# @meta Range
50- # @brief
51- # @details
65+ # @brief Create a list based on a range and a filter function
66+ # @details The range is unmodified.
5267# ---
68+ # (let obj (range 1 10))
69+ # (print (range:filter obj math:even)) # [2 4 6 8]
5370# ---
54- # @param
71+ # @param _range the range object
72+ # @param _fun the filter function
5573# @author https://github.com/SuperFola
5674###
5775(let range:filter (fun (_range _fun) {
6179 (if (_fun _value) (set _output (append _output _value)))
6280 (set _value (_range))
6381 })
82+ (_range.reset)
6483
6584 _output
6685}))
6786
6887###
6988# @meta Range
70- # @brief
71- # @details
89+ # @brief Create a list based on a range and a function to apply to each elements
90+ # @details The range is unmodified.
7291# ---
92+ # (let obj (range 1 10))
93+ # (print (range:map obj (fun (e) (* e e)))) # [1 4 9 16 25 36 49 64 81]
7394# ---
74- # @param
95+ # @param _range the range object
96+ # @param _fun the function to apply
7597# @author https://github.com/SuperFola
7698###
7799(let range:map (fun (_range _fun) {
81103 (set _output (append _output (_fun _value)))
82104 (set _value (_range))
83105 })
106+ (_range.reset)
84107
85108 _output
86109}))
87110
88111###
89112# @meta Range
90- # @brief
91- # @details
113+ # @brief Create a reduced list based on a range and a reduction function
114+ # @details The range is unmodified.
92115# ---
116+ # (let obj (range 1 10))
117+ # (print (range:reduce obj (fun (e) (+ e e)))) # 45
93118# ---
94- # @param
119+ # @param _range the range object
120+ # @param _fun the reduction function
95121# @author https://github.com/SuperFola
96122###
97123(let range:reduce (fun (_range _fun) {
101127 (set _output (_fun _output _last))
102128 (set _last (_range))
103129 })
130+ (_range.reset)
104131 _output
105132}))
0 commit comments