Created
August 27, 2013 13:45
-
-
Save jccc/6353725 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 queue | |
[& c] | |
(into | |
(clojure.lang.PersistentQueue/EMPTY) | |
c)) | |
(defn k-combos | |
[k r] | |
(if (zero? k) nil | |
(letfn | |
[(expand | |
[s] | |
(let [i (peek s)] | |
(into | |
(pop s) | |
(for [j (range (last i) r)] | |
(conj i j))))) | |
(step | |
[s] | |
(when-not | |
(empty? s) | |
(if (= | |
(count | |
(peek s)) | |
k) | |
(cons | |
(peek s) | |
(lazy-seq | |
(step (pop s)))) | |
(lazy-seq | |
(step (expand s))))))] | |
(step | |
(into | |
(queue) | |
(map | |
vector | |
(range r))))))) | |
;tree: | |
;[ [2] [0 1] [1] [2] [1 1] ...] | |
(defn expand-tree | |
[t] | |
(for [c | |
(rest | |
(k-combos | |
(reduce + (last t)) | |
3))] | |
(conj t c))) | |
(def all-trees | |
(letfn | |
[(step | |
[s] | |
(when-not | |
(empty? s) | |
(cons | |
(peek s) | |
(lazy-seq | |
(step | |
(into | |
(pop s) | |
(expand-tree | |
(peek s))))))))] | |
(step | |
(queue [[1]] [[2]])))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment