Skip to content

Instantly share code, notes, and snippets.

@christianromney
Created December 18, 2014 23:07
Show Gist options
  • Save christianromney/dc6eaf72e28a4f1cc9b2 to your computer and use it in GitHub Desktop.
Save christianromney/dc6eaf72e28a4f1cc9b2 to your computer and use it in GitHub Desktop.
(defn recursive-take
"Recursively take from a list based on the operation list
For example [:h :l :l :h] will take using the following operations
until the list is exhausted: first last last first."
[col operations]
(loop [c col ops (cycle operations) acc []]
(if (empty? c)
acc
(let [op (first ops)
[head-fn tail-fn] (if (= op :h)
[first rest]
[last drop-last])]
(recur (tail-fn c) (rest ops) (concat [(head-fn c)] acc))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment