Skip to content

Commit ffbb082

Browse files
committed
Refactored iterate into type and added IReduce
1 parent 3b9d76a commit ffbb082

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

pixie/stdlib.pxi

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3075,18 +3075,20 @@ ex: (vary-meta x assoc :foo 42)"
30753075

30763076
(deftype Iterate [f x]
30773077
IReduce
3078-
(-reduce [self f init]
3079-
(loop [acc (f (if (nil? init)
3080-
(first self)
3081-
init))]
3078+
(-reduce [self rf init]
3079+
(loop [col (rest self)
3080+
acc (rf init (first self))]
30823081
(if (reduced? acc)
30833082
@acc
3084-
(recur (f acc))))))
3083+
(recur (rest col) (rf acc (first col))))))
3084+
ISeq
3085+
(-seq [self]
3086+
(cons x (lazy-seq* (fn [] (->Iterate f (f x)))))))
30853087

30863088
(defn iterate
30873089
{:doc "Returns a lazy sequence of x, (f x), (f (f x)) etc. f must be free of
30883090
side-effects"
30893091
:signatures [[f x]]
30903092
:added "0.1"}
30913093
[f x]
3092-
(lazy-seq (cons x (iterate f (f x)))))
3094+
(->Iterate f x))

tests/pixie/tests/test-stdlib.pxi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -764,5 +764,6 @@
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>")
768-
(t/assert= (reduce (fn [a v] (if (< a 10) (+ a v) (reduced a))) (iterate (partial + 2) 1)) 16))
767+
(t/assert= (reduce (fn [a v] (reduced "foo")) 0 (iterate inc 1)) "foo")
768+
(t/assert= (reduce (fn [a v] (if (< a 10) (+ a v) (reduced a))) 0 (iterate (partial + 2) 1)) 16))
769+

0 commit comments

Comments
 (0)