File tree Expand file tree Collapse file tree 2 files changed +38
-2
lines changed Expand file tree Collapse file tree 2 files changed +38
-2
lines changed Original file line number Diff line number Diff line change 357
357
# =begin
358
358
# (let lst [1 2 3 4 5 6 7 8 9])
359
359
# (let is_even (fun (e) (= 0 (mod e 2))))
360
- # (print (count_if lst is_even)) # 4
360
+ # (print (countIf lst is_even)) # 4
361
361
# =end
362
362
# @author https://github.com/SuperFola
363
363
(let countIf (fun (_L _f) {
371
371
acc))
372
372
acc)))
373
373
(_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)))
Original file line number Diff line number Diff line change 76
76
77
77
(test:eq (list:countIf a (fun (e) (= 0 (mod e 2)))) 1)
78
78
(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]) })
You can’t perform that action at this time.
0 commit comments