THX1137
THX1137

Reputation: 983

How do I cycle over a collection from a specified point in Clojure?

How would a cycle over a collection but start at different specified points in that collection? That is, if I had [“a” “b” “c” “d” “e”] be able to start the cycle at, say, “c” by specifying to start at the third position (or perhaps second, if it needs to be zero indexed)?

Upvotes: 1

Views: 127

Answers (1)

Denis Fuenzalida
Denis Fuenzalida

Reputation: 3346

You can drop a few elements from a collection that cycles over:

user=> (def elems [:a :b :c :d :e])
#'user/elems

user=> (->> elems cycle (drop 2) (take 10))
(:c :d :e :a :b :c :d :e :a :b)

Upvotes: 8

Related Questions