File tree Expand file tree Collapse file tree 2 files changed +12
-1
lines changed
Expand file tree Collapse file tree 2 files changed +12
-1
lines changed Original file line number Diff line number Diff 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"
Original file line number Diff line number Diff line change 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 ))
You can’t perform that action at this time.
0 commit comments