Skip to content

Commit 8342c1e

Browse files
committed
Merge pull request #480 from stuarth/impl-cycle
add cycle
2 parents b3bad5f + 7a0c690 commit 8342c1e

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pixie/stdlib.pxi

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,6 +1746,19 @@ The new value is thus `(apply f current-value-of-atom args)`."
17461746
s)))]
17471747
(lazy-seq (step pred coll)))))
17481748

1749+
(defn cycle
1750+
[coll]
1751+
(if (empty? coll)
1752+
()
1753+
(let [cycle'
1754+
(fn cycle' [current]
1755+
(lazy-seq
1756+
(cons
1757+
(first current)
1758+
(let [rst (rest current)]
1759+
(cycle' (if (empty? rst) coll rst))))))]
1760+
(cycle' coll))))
1761+
17491762
;; TODO: use a transient map in the future
17501763
(defn group-by
17511764
{:doc "Groups the collection into a map keyed by the result of applying f on each element. The value at each key is a vector of elements in order of appearance."

tests/pixie/tests/test-stdlib.pxi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,12 @@
568568
(t/assert= (transduce (drop-while even?) conj [0 2] [1 4 6]) [0 2 1 4 6])
569569
(t/assert= (transduce (drop-while even?) conj [0 2] [2 4 6 7 8]) [0 2 7 8]))
570570

571+
(t/deftest test-cycle
572+
(t/assert= (cycle ()) ())
573+
(t/assert= (cycle nil) ())
574+
(t/assert= (take 5 (cycle '(1 2))) '(1 2 1 2 1))
575+
(t/assert= (take 3 (cycle [nil])) '(nil nil nil)))
576+
571577
(t/deftest test-trace
572578
(try
573579
(/ 0 0)

0 commit comments

Comments
 (0)