Search code examples
clojureclojurescript

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)?


Solution

  • 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)