formulae v0.8.0 Formulae.Combinators View Source

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]]

Link to this section 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

Link to this section Functions

Link to this macro

combinations(l, n)

View Source (macro)

Specs

combinations(list :: list(), count :: non_neg_integer()) :: [list()]

Calculates all combinations of the list, given as the first parameter

Link to this macro

permutations(l, n)

View Source (macro)

Specs

permutations(list :: list(), count :: non_neg_integer()) :: [list()]

Calculates all permutations of the list, given as the first parameter

Link to this macro

repeated_permutations(l, n)

View Source (macro)

Specs

repeated_permutations(list :: list(), count :: non_neg_integer()) :: [list()]

Calculates all repeated permutations of the list, given as the first parameter