Skip to content

Instantly share code, notes, and snippets.

@hans
Created May 1, 2011 19:31
Show Gist options
  • Save hans/950781 to your computer and use it in GitHub Desktop.
Save hans/950781 to your computer and use it in GitHub Desktop.
a function which will split a sequence into two parts
;; (split 3 [1 2 3 4 5 6]) => [[1 2 3] [4 5 6]]
;; (split 1 [:a :b :c :d]) => [[:a] [:b :c :d]]
(defn split [n xs]
(loop [acc '(), inacc '(), flxs (first xs), lxs (rest xs), cnt n]
(print acc inacc cnt "\n")
(if (nil? flxs)
(reverse (conj acc (reverse inacc)))
(if (zero? cnt)
(recur (conj acc (reverse inacc)) nil flxs lxs -1)
(recur acc (conj inacc flxs) (first lxs) (rest lxs)
(if (> cnt 0) (dec cnt) -1))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment