Skip to content

Commit 6002ef0

Browse files
committed
merge master
2 parents 8bcad2f + 9543c07 commit 6002ef0

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ all: help
22

33
EXTERNALS=externals
44

5-
PYTHON ?= python2
5+
PYTHON ?= `env which -a python2 python2.7 | head -n1`
66
PYTHONPATH=$$PYTHONPATH:$(EXTERNALS)/pypy
77

88

pixie/stdlib.pxi

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3079,3 +3079,23 @@ ex: (vary-meta x assoc :foo 42)"
30793079
:signatures [[rexegp-str opts]]}
30803080
[regexp-str opts]
30813081
(println (str regexp-str " " opts)))
3082+
3083+
(deftype Iterate [f x]
3084+
IReduce
3085+
(-reduce [self rf init]
3086+
(loop [col (rest self)
3087+
acc (rf init (first self))]
3088+
(if (reduced? acc)
3089+
@acc
3090+
(recur (rest col) (rf acc (first col))))))
3091+
ISeq
3092+
(-seq [self]
3093+
(cons x (lazy-seq* (fn [] (->Iterate f (f x)))))))
3094+
3095+
(defn iterate
3096+
{:doc "Returns a lazy sequence of x, (f x), (f (f x)) etc. f must be free of
3097+
side-effects"
3098+
:signatures [[f x]]
3099+
:added "0.1"}
3100+
[f x]
3101+
(->Iterate f x))

tests/pixie/tests/test-stdlib.pxi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,3 +761,9 @@
761761
(t/deftest test-memoize
762762
(let [f (memoize rand)]
763763
(t/assert= (f) (f))))
764+
765+
(t/deftest test-iterate
766+
(t/assert= (take 5 (iterate inc 5)) '(5 6 7 8 9))
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)