Skip to content

Commit 8d14b27

Browse files
committed
feat(list): adding list:iterate and list:iota
1 parent 30f275a commit 8d14b27

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

List.ark

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@
357357
# =begin
358358
# (let lst [1 2 3 4 5 6 7 8 9])
359359
# (let is_even (fun (e) (= 0 (mod e 2))))
360-
# (print (count_if lst is_even)) # 4
360+
# (print (countIf lst is_even)) # 4
361361
# =end
362362
# @author https://github.com/SuperFola
363363
(let countIf (fun (_L _f) {
@@ -371,3 +371,34 @@
371371
acc))
372372
acc)))
373373
(_inner _L _f 0) }))
374+
375+
# @brief Generate a sequence based on a unary function, initial value and length
376+
# @param _init initial value of the sequence
377+
# @param _f unary function to generate values
378+
# @param _length the sequence length
379+
# =begin
380+
# (let f (fun (x) (+ 7 x)))
381+
# (print (iterate 0 f 10)) # [0 7 14 21 28 35 42 49 56 63]
382+
# =end
383+
# @author https://github.com/SuperFola
384+
(let iterate (fun (_init _f _length) {
385+
(assert (> _length 0) "list:iterate needs a length of at least 1")
386+
387+
(mut _output [_init])
388+
(mut _last _init)
389+
(mut _i 1)
390+
(while (< _i _length) {
391+
(set _last (_f _last))
392+
(append! _output _last)
393+
(set _i (+ 1 _i)) })
394+
_output }))
395+
396+
# @brief Generate a sequence of numbers
397+
# @param _init initial value of the sequence
398+
# @param _length the sequence length
399+
# =begin
400+
# (print (iota 0 10)) # [0 1 2 3 4 5 6 7 8 9]
401+
# =end
402+
# @author https://github.com/SuperFola
403+
(let iota (fun (_init _length)
404+
(iterate _init (fun (x) (+ 1 x)) _length)))

tests/list-tests.ark

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@
7676

7777
(test:eq (list:countIf a (fun (e) (= 0 (mod e 2)))) 1)
7878
(test:eq (list:countIf a (fun (e) (= 1 (mod e 2)))) 2)
79-
(test:eq (list:countIf [] (fun (e) (= 1 (mod e 2)))) 0)})
79+
(test:eq (list:countIf [] (fun (e) (= 1 (mod e 2)))) 0)
80+
81+
(test:eq (list:iterate 0 (fun (x) (+ 1 x)) 5) [0 1 2 3 4])
82+
(test:eq (list:iterate 0 (fun (x) (+ 1 x)) 5) (list:iota 0 5))
83+
(test:eq (list:iterate "" (fun (x) (+ x "a")) 5) ["" "a" "aa" "aaa" "aaaa"])
84+
(test:eq (list:iterate 0 (fun (x) (+ 1 x)) 1) [0]) })

0 commit comments

Comments
 (0)