Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(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
You can’t perform that action at this time.