Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pixie/stdlib.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -1746,6 +1746,19 @@ The new value is thus `(apply f current-value-of-atom args)`."
s)))]
(lazy-seq (step pred coll)))))

(defn cycle
[coll]
(if (empty? coll)
()
(let [cycle'
(fn cycle' [current]
(lazy-seq
(cons
(first current)
(let [rst (rest current)]
(cycle' (if (empty? rst) coll rst))))))]
(cycle' coll))))

;; TODO: use a transient map in the future
(defn group-by
{: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."
Expand Down
6 changes: 6 additions & 0 deletions tests/pixie/tests/test-stdlib.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,12 @@
(t/assert= (transduce (drop-while even?) conj [0 2] [1 4 6]) [0 2 1 4 6])
(t/assert= (transduce (drop-while even?) conj [0 2] [2 4 6 7 8]) [0 2 7 8]))

(t/deftest test-cycle
(t/assert= (cycle ()) ())
(t/assert= (cycle nil) ())
(t/assert= (take 5 (cycle '(1 2))) '(1 2 1 2 1))
(t/assert= (take 3 (cycle [nil])) '(nil nil nil)))

(t/deftest test-trace
(try
(/ 0 0)
Expand Down