Skip to content

Commit c94144b

Browse files
author
angusiguess
committed
Make sequence lazy, make educe seq use sequence
1 parent 1626fff commit c94144b

File tree

3 files changed

+34
-31
lines changed

3 files changed

+34
-31
lines changed

pixie/stdlib.pxi

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,7 +1907,7 @@ not enough elements were present."
19071907

19081908
ISeqable
19091909
(-seq [self]
1910-
(seq (reduce conj self))))
1910+
(sequence xform coll)))
19111911

19121912
(defn eduction
19131913
"Returns a reducible/iterable application of the transducers
@@ -2189,12 +2189,18 @@ For more information, see http://clojure.org/special_forms#binding-forms"}
21892189
(filter (complement pred) coll)))
21902190

21912191
(defn sequence
2192-
"Returns a lazy sequence of `data`, optionally transforming it using `xform`.
2193-
Given an `eduction`, produces a lazy sequence of it."
2194-
([eduction]
2195-
(when (seq eduction) (lazy-seq (cons (first eduction) (sequence (rest eduction))))))
2196-
([xform data]
2197-
(sequence (eduction xform data))))
2192+
"Returns a lazy sequence of `data`, optionally transforming it using `xform`"
2193+
([coll]
2194+
(if (seq? coll) coll
2195+
(or (seq coll) ())))
2196+
([xform coll]
2197+
(let [step (defn step [xform acc xs]
2198+
(if-let [s (seq xs)]
2199+
(let [next-acc ((xform conj) acc (first s))]
2200+
(if (= acc next-acc) (step xform next-acc (next s))
2201+
(concat (drop (count acc) next-acc) (step xform next-acc (next s)))))
2202+
nil))]
2203+
(lazy-seq (step xform [] coll)))))
21982204

21992205
(defn distinct
22002206
{:doc "Returns the distinct elements in the collection."

tests/pixie/test-sequence.pxi

Lines changed: 0 additions & 23 deletions
This file was deleted.

tests/pixie/tests/test-stdlib.pxi

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@
595595
(seq)))
596596
;; expanding transducer with nils
597597
(t/assert= '(1 2 3 nil 4 5 6 nil)
598-
(eduction cat [[1 2 3 nil] [4 5 6 nil]])))
598+
(seq (eduction cat [[1 2 3 nil] [4 5 6 nil]]))))
599599

600600
(t/deftest test-trace
601601
(try
@@ -767,3 +767,23 @@
767767
(t/assert= (reduce (fn [a v] (reduced "foo")) 0 (iterate inc 1)) "foo")
768768
(t/assert= (reduce (fn [a v] (if (< a 10) (+ a v) (reduced a))) 0 (iterate (partial + 2) 1)) 16))
769769

770+
(t/deftest test-sequence-empty-sequences
771+
(t/assert= '() (take 1 (sequence (map inc) '())))
772+
(t/assert= '() (take 1 (sequence (map inc) [])))
773+
(t/assert= '() (take 1 (sequence (map inc) #{})))
774+
(t/assert= '() (take 1 (sequence (map inc) {}))))
775+
776+
(t/deftest test-sequence-non-empty-sequences
777+
(t/assert= '(1 3) (take 2 (sequence (comp
778+
(filter even?)
779+
(map inc)) (range 3))))
780+
(t/assert= '(1) (take 1 (sequence (distinct) (repeat 4 1)))))
781+
782+
(t/deftest test-sequence-early-terminating-sequences
783+
(t/assert= '() (take 5 (sequence (filter (fn [x] false)) (repeat 8 8))))
784+
(t/assert= '(1 2) (take 3 (sequence (map identity) [1 2])))
785+
(t/assert= #{[:a 1] [:b 2]} (into #{} (take 3 (sequence (filter (fn [[k v]]
786+
(keyword? k)) {:a 1
787+
:b 2
788+
"c" 3
789+
"d" 4}))))))

0 commit comments

Comments
 (0)