Skip to content

Instantly share code, notes, and snippets.

@jccc
Created August 27, 2013 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jccc/6353725 to your computer and use it in GitHub Desktop.
Save jccc/6353725 to your computer and use it in GitHub Desktop.
(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