Skip to content

Commit 3b9d76a

Browse files
committed
Added IReduce to iterate
1 parent 7b0680b commit 3b9d76a

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pixie/stdlib.pxi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3073,6 +3073,16 @@ ex: (vary-meta x assoc :foo 42)"
30733073
ret)
30743074
val)))))
30753075

3076+
(deftype Iterate [f x]
3077+
IReduce
3078+
(-reduce [self f init]
3079+
(loop [acc (f (if (nil? init)
3080+
(first self)
3081+
init))]
3082+
(if (reduced? acc)
3083+
@acc
3084+
(recur (f acc))))))
3085+
30763086
(defn iterate
30773087
{:doc "Returns a lazy sequence of x, (f x), (f (f x)) etc. f must be free of
30783088
side-effects"

tests/pixie/tests/test-stdlib.pxi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,4 +764,5 @@
764764

765765
(t/deftest test-iterate
766766
(t/assert= (take 5 (iterate inc 5)) '(5 6 7 8 9))
767-
(t/assert= (str (type (iterate inc 1))) "<type pixie.stdlib.LazySeq>"))
767+
(t/assert= (str (type (iterate inc 1))) "<type pixie.stdlib.LazySeq>")
768+
(t/assert= (reduce (fn [a v] (if (< a 10) (+ a v) (reduced a))) (iterate (partial + 2) 1)) 16))

0 commit comments

Comments
 (0)