Utilities for generating permutations of sequences.
Utilities for generating permutations of sequences.
(cartesian-product colls)
Accepts a sequence of collections colls
and returns a lazy sequence of the
cartesian product of all collections.
The cartesian product of N collections is a sequences of sequences, each N
long, of every possible way of choosing N
items where the first comes from
the first entry in colls
, the second from the second entry and so on.
NOTE: This implementation comes from Alan Malloy at this StackOverflow post. Thanks, Alan!
Accepts a sequence of collections `colls` and returns a lazy sequence of the cartesian product of all collections. The cartesian product of N collections is a sequences of sequences, each `N` long, of every possible way of choosing `N` items where the first comes from the first entry in `colls`, the second from the second entry and so on. NOTE: This implementation comes from Alan Malloy at this [StackOverflow post](https://stackoverflow.com/a/18248031). Thanks, Alan!
(combinations xs p)
Returns a lazy sequence of every possible set of p
elements chosen from
xs
.
Returns a lazy sequence of every possible set of `p` elements chosen from `xs`.
(factorial n)
Returns the factorial of n
, ie, the product of 1 to n
(inclusive).
Returns the factorial of `n`, ie, the product of 1 to `n` (inclusive).
(list-interchanges permuted-list original-list)
Given a permuted-list
and the original-list
, returns the number of
interchanges required to generate the permuted list from the original list.
Given a `permuted-list` and the `original-list`, returns the number of interchanges required to generate the permuted list from the original list.
(number-of-combinations n k)
Returns the number of possible ways of choosing k
distinct elements from a
collection of n
total items.
Returns the number of possible ways of choosing `k` distinct elements from a collection of `n` total items.
(number-of-permutations n)
Returns the number of possible ways of permuting a collection of n
distinct
elements.
Returns the number of possible ways of permuting a collection of `n` distinct elements.
(permutation-parity permuted-list original-list)
Given a permuted-list
and the original-list
, returns the parity (1 for
even, -1 for odd) of the number of the number of interchanges required to
generate the permuted list from the original list.
If the two items
Given a `permuted-list` and the `original-list`, returns the parity (1 for even, -1 for odd) of the number of the number of interchanges required to generate the permuted list from the original list. If the two items
(permutation-sequence as)
Produces an iterable sequence developing the permutations of the input sequence of objects (which are considered distinct) in church-bell-changes order - that is, each permutation differs from the previous by a transposition of adjacent elements (Algorithm P from §7.2.1.2 of Knuth).
This is an unusual way to go about this in a functional language, but it's fun.
This approach has the side-effect of arranging for the parity of the generated permutations to alternate; the first permutation yielded is the identity permutation (which of course is even).
Inside, there is a great deal of mutable state, but this cannot be observed by the user.
Produces an iterable sequence developing the permutations of the input sequence of objects (which are considered distinct) in church-bell-changes order - that is, each permutation differs from the previous by a transposition of adjacent elements (Algorithm P from §7.2.1.2 of Knuth). This is an unusual way to go about this in a functional language, but it's fun. This approach has the side-effect of arranging for the parity of the generated permutations to alternate; the first permutation yielded is the identity permutation (which of course is even). Inside, there is a great deal of mutable state, but this cannot be observed by the user.
(permutations xs)
Returns a lazy sequence of every possible arrangement of the elements of xs
.
Returns a lazy sequence of every possible arrangement of the elements of `xs`.
(permute permutation xs)
Given a permutation
(represented as a list of numbers), and a sequence xs
to be permuted, construct the list so permuted.
Given a `permutation` (represented as a list of numbers), and a sequence `xs` to be permuted, construct the list so permuted.
(sort-and-permute ulist <? cont)
cont = (fn [ulist slist perm iperm] ...)
Given a short list and a comparison function, to sort the list by the comparison, returning the original list, the sorted list, the permutation procedure and the inverse permutation procedure developed by the sort.
cont = (fn [ulist slist perm iperm] ...) Given a short list and a comparison function, to sort the list by the comparison, returning the original list, the sorted list, the permutation procedure and the inverse permutation procedure developed by the sort.
(subpermute m xs)
Given a sequence xs
and a map m
of replacement indices, returns a new
version of xs
with the element at the position marked by each key in m
replaced by the element at each value in the original xs
.
Given a sequence `xs` and a map `m` of replacement indices, returns a new version of `xs` with the element at the position marked by each key in `m` replaced by the element at each value in the original `xs`.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close