View Source Formulae.Combinators (formulae v0.15.2)
Functions to calculate all the combinations, permutations and repeated permutations of the list given.
Examples
iex> Formulae.Combinators.combinations(~w|a b c d|a, 2)
[[:a, :b], [:a, :c], [:a, :d], [:b, :c], [:b, :d], [:c, :d]]
iex> Formulae.Combinators.permutations(~w|a b c d|a, 2)
[[:a, :b], [:a, :c], [:a, :d], [:b, :a], [:b, :c], [:b, :d],
[:c, :a], [:c, :b], [:c, :d], [:d, :a], [:d, :b], [:d, :c]]
iex> Formulae.Combinators.repeated_permutations(~w|a b c d|a, 2)
[[:a, :a], [:a, :b], [:a, :c], [:a, :d], [:b, :a], [:b, :b],
[:b, :c], [:b, :d], [:c, :a], [:c, :b], [:c, :c], [:c, :d],
[:d, :a], [:d, :b], [:d, :c], [:d, :d]]
NB this functions should not be used for relatively big n
because
they perform greedy evaluation. See to Formulae.Combinators.Stream
for lazy analogues returning streams.
Dynamic numbers
To use combinations and permutations with dynamic number n
, use
Formulae.combinations/2
and Formulae.permutations/2
, generated
in the compile time. By default, there are 42
clauses for combinations
and 12
for permutations
are generated. They might be changed in config
,
keys :formulae, :max_combinations
and :formulae, :max_permutations
. To
suppress generation, use :formulae, :generate_combinators, false
.
iex> with n <- 2, do: Formulae.combinations(~w|a b c d|a, n)
[[:a, :b], [:a, :c], [:a, :d], [:b, :c], [:b, :d], [:c, :d]]
Summary
Functions
Calculates all combinations of the list, given as the first parameter
Calculates all permutations of the list, given as the first parameter
Calculates all repeated permutations of the list, given as the first parameter
Functions
@spec combinations(list :: list(), count :: non_neg_integer()) :: [list()]
Calculates all combinations of the list, given as the first parameter
@spec permutations(list :: list(), count :: non_neg_integer()) :: [list()]
Calculates all permutations of the list, given as the first parameter
@spec repeated_permutations(list :: list(), count :: non_neg_integer()) :: [list()]
Calculates all repeated permutations of the list, given as the first parameter