Created
March 29, 2011 00:31
-
-
Save mecdemort/891606 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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