Skip to content

Commit

Permalink
Add cycle function for cycled lazy sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
profitware committed Oct 10, 2016
1 parent bad3c20 commit 1228f6f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/clj.lfe
Expand Up @@ -515,6 +515,11 @@
only computes subseqeuent values as needed."
(fn [] (cons start (next func (funcall func start step) step))))

(defn cycle
"Return a lazy list with all of its elements cycled."
([()] ())
([lst] (fn [] (-cycle lst ()))))

(defn range []
"Equivalent to `(range 1 1)`."
(range 1 1))
Expand Down Expand Up @@ -702,3 +707,9 @@
(flet ((maps-get [k m] (call 'maps 'get k m not-found)))
(-get-in #'maps-get/2 xmap keys not-found)))
([_xmap _keys not-found] not-found))

(defn- -cycle
([() ()] ())
([() lst] (-cycle (lists:reverse lst) ()))
([`(,head . ,tail) lst]
(cons head (fn [] (-cycle tail (cons head lst))))))
18 changes: 18 additions & 0 deletions test/clj-tests.lfe
Expand Up @@ -516,6 +516,24 @@
([3 _] 4)
([4 _] (error 'overzealous-take)))))))
(deftest cycle-and-take
(are* [x y] (ok? (is-match x y))
()
(clj:cycle ())
'(1 2 3)
(clj:take 3 (clj:cycle '(1 2 3)))
'(1 2)
(clj:take 2 (clj:cycle '(1 2 3)))
'(1 2 3 1)
(clj:take 4 (clj:cycle '(1 2 3)))
'(1 2 3 1 2 3 1)
(clj:take 7 (clj:cycle '(1 2 3)))))
(deftest range
(are* [x y] (ok? (is-match x y))
1 (car (funcall (clj:range)))
Expand Down

0 comments on commit 1228f6f

Please sign in to comment.