combi-tree.combi-tree
Combinatorics functions based on building a combination tree.
combinations
(combinations tree)(combinations coll n)
Returns a lazy sequence of all the unique ways of taking n different elements
from a sequence (i.e. elements at n distinct positions in the sequence) while
preserving their original order. The same combination may be returned multiple
times if coll contains duplicates.
Returns '(()) if n is 0.
This function also takes a single combinations tree, thus deriving all
combinations the tree can yield. The given tree can be the the result of
combinations-tree or distinct-combinations-tree. In the first case duplicates
may appear if the original collection contains duplicates. In the latter case
only distinct combinations will be returned (no duplicates).
Returns nil if no combinations exists, if tree or coll is nil or empty,
or if n is 0.
Throws the same exceptions as combinations-tree.
Throws an exception when n is negative or greater than the size of coll.
With 2 args this returns what clojure.math.combinatorics/combinations returns
except when n=0 where it returns nil instead of '(()).
combinations-tree
(combinations-tree coll)(combinations-tree coll n)
Returns nested and mostly lazy sequences representing a tree of all the
unique ways of taking n different elements from a sequence (i.e. elements at
n distinct positions in the sequence) while preserving their original order.
A branch in the tree is a sequence of at least 2 items, the first being
the node and the rest being the children of that node. Nodes and leafs are
items of the original collection so that each path in the tree provides a
possible combination. The result of this function is a sequence of root
children, meaning there is no root node in the first position.
If the original collection contains duplicates, the resulting combinations
will also contain duplicates. Use distinct-combinations-tree to get a tree
that yields no duplicates.
Examples:
(combinations-tree [:k :s :k :s] 2)
=> ((:k :s :k :s) (:s :k :s) (:k :s))
(combinations-tree [:k :s :k :s] 3)
=> ((:k (:s :k :s) (:k :s)) (:s (:k :s)))
When only coll is provided as argument, this function returns a map of
all combinations trees indexed by their size:
(combinations-tree [:k :s :k :s])
=> {0 ()
1 (:k :s :k :s),
2 ((:k :s :k :s) (:s :k :s) (:k :s)),
3 ((:k (:s :k :s) (:k :s)) (:s (:k :s))),
4 ((:k (:s (:k :s))))}
Returns nil if n is 0 or greater than the size of coll.
Throws an exception if n is negative or nil.combinations-zip
(combinations-zip combinations-tree)
Returns a clojure.zip zipper on a given combinations tree. Operates on the
result of combinations-tree or distinct-combinations-tree. Note that because
combination trees have no root node, a dummy nil root node is added to the
tree passed as argument so that a zipper can be built with it.
distinct-combinations
(distinct-combinations coll n)
Returns a lazy sequence of all the unique ways of taking n different elements
from a sequence (i.e. elements at n distinct positions in the sequence) while
preserving their original order. A combination is returned only once even if
coll contains duplicates.
Returns nil if no combinations exists, if tree or coll is nil or empty,
or if n is 0.
Throws the same exceptions as combinations-tree.
Throws an exception when n is negative or greater than the size of coll.
distinct-combinations-tree
(distinct-combinations-tree coll-or-combinations-tree)(distinct-combinations-tree coll n)
Same as combinations-tree, except that trees corresponding to duplicate
combinations are merged into a single path terminating by a ::dups leaf.
This function can accept a single parameter, which can either be a collection
for which combinations tree must be computed, or a tree as returned by
combinations-tree which must satisfy tree-seq?.
prefix-tree
(prefix-tree heads tree)
Adds a sequence of items to the head of a given tree, returning a new tree
that yields sequences starting with the sequence of items. For example:
(combinations (prefix-tree [1 2] (combinations-tree [3 4 5 6] 2)))
=> ((1 2 3 4) (1 2 3 5) (1 2 3 6) (1 2 4 5) (1 2 4 6) (1 2 5 6))
tree->combinations
(tree->combinations tree)(tree->combinations tree remove-dups?)
Returns a lazy sequence of the combinations derived from a tree as returned
by combinations-tree or distinct-combinations-tree. An optional boolean
indicates if the result should include combinations derived from paths
terminating by ::dups.
tree-conj
(tree-conj coll x)(tree-conj coll x & xs)
Add a marker meta-data to the result of calling conj on the given arguments.
Used in combination trees to distinguishing sequences that are part of the
tree structure from sequences that are node values.
tree-cons
(tree-cons x seq)
Creates a cons with a marker meta-data and containing the given elements.
Used in combination trees to distinguish sequences that are part of the
tree structure from sequences that are node values.
tree-list
(tree-list & elements)
Creates a list with a marker meta-data and containing the given elements.
Used in combination trees to distinguish sequences that are part of the
tree structure from sequences that are node values.
tree-rest
(tree-rest coll)
Returns the rest of the given coll with a marker meta-data.
Used in combination trees to distinguish sequences that are part of the
tree structure from sequences that are node values.
tree-seq?
(tree-seq? o)
Returns true if the given object is a seq with a ::tree meta-data.
unique-combinations
(unique-combinations distinct-combinations-tree)(unique-combinations coll n)
Computes all unique combinations of n different elements taken from a
sequence (i.e. elements at n distinct positions in the sequence) while
preserving their original order.
When a coll and a single integer is provided, this function returns a sequence
of all combinations of n elements from coll:
(unique-combinations [:k :s :k :s] 2)
=> ((:s :k) (:s :s) (:k :k))
When coll contains duplicates, the set of all possible combinations may also
contain duplicates which are eliminated from the results of this function as
they are not unique. In the above example the (:k :s) combination has been
removed for that reason because it matches positions 1+2 and 3+4.
When duplicates are not be be removed, use the combinations function instead.
This function also takes a single tree as returned by distinct-combinations-tree
thus deriving all unique combinations the tree can yield.
Returns nil if no combinations exists, if tree or coll is nil or empty,
or if n is 0.
Throws the same exceptions as distinct-combinations-tree.