Skip to content

Instantly share code, notes, and snippets.

@mecdemort
Created March 29, 2011 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mecdemort/891606 to your computer and use it in GitHub Desktop.
Save mecdemort/891606 to your computer and use it in GitHub Desktop.
(defn partitions [sizes coll]
(lazy-seq
(when-let [n (first sizes)]
(when-let [s (seq coll)]
(cons (take n coll)
(partitions (next sizes) (drop n coll)))))))
(defn partitions [sizes coll]
(second (reduce
(fn [[coll res] n]
[(drop n coll) (conj res (vec (take n coll)))])
[coll []] sizes)))
(defn partitions [sizes coll]
(->> sizes
(reductions (fn [[_ coll] part]
(split-at part coll))
[nil coll] ,)
next
(map first ,)
(take-while seq ,))
(defn partitions [sizes coll]
(unfold (fn [[[n & sizes] coll]]
[(take n coll) [sizes (drop n coll)]])
(partial not-any? seq)
[sizes coll]))
user> (partitions [1 2 3 2 1] [1 2 3 4 5 6 7 8 9])
((1) (2 3) (4 5 6) (7 8) (9))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment