Created
March 16, 2017 16:46
-
-
Save paultopia/85d276dd5ea7ddfe6135da551b580e4a to your computer and use it in GitHub Desktop.
Replacement for clojure.math.combinatorics/partitions that preserves ordering --- see discussion at https://groups.google.com/forum/m/#!topic/clojure/DoEMT45VpAo
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
| (require '[clojure.math.combinatorics :as c]) | |
| (defn breaks->partition | |
| ([v brks] | |
| (breaks->partition 0 [] v brks)) | |
| ([start pars v brks] | |
| (if (empty? brks) | |
| (conj pars (subvec v start (count v))) | |
| (let [this-part (subvec v start (first brks))] | |
| (recur (first brks) (conj pars this-part) v (rest brks)))))) | |
| (defn min-parts [min splits] | |
| (>= (count splits) (- min 1))) | |
| (defn max-parts [max splits] | |
| (<= (count splits) (- max 1))) | |
| (defn ordered-partitions [v & {:keys [max min]}] | |
| (let | |
| [s (c/subsets (range 1 (count v))) | |
| fs (cond | |
| (and max min) | |
| (filter | |
| (partial max-parts max) | |
| (filter (partial min-parts min) s)) | |
| max (filter (partial max-parts max) s) | |
| min (filter (partial min-parts min) s) | |
| :else s)] | |
| (map (partial breaks->partition v) fs))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment