Combination v0.0.3 Combination View Source

Provide a set of algorithms to generate combinations and permutations.

For source collection containing non-distinct elements, pipe the resultant list through Enum.uniq/1 to remove duplicate elements.

Link to this section Summary

Functions

Generate combinations based on given collection

Generate all permutation of the collection, filtered by filter function

Link to this section Functions

Link to this function combine(collection, k) View Source
combine(Enum.t, non_neg_integer) :: [list]

Generate combinations based on given collection.

Examples

iex> 1..3 |> Combination.combine(2)
[[3, 2], [3, 1], [2, 1]]
Link to this function permutate(collection, filter \\ fn _p -> true end) View Source
permutate(Enum.t, (list -> as_boolean(term))) :: [list]

Generate all permutation of the collection, filtered by filter function.

The filter function filters the generated permutation before it is added to the result list. It returns true by default, thus allowing all permutations.

Example

iex> 1..3 |> Combination.permutate
[[1, 2, 3], [2, 1, 3], [3, 1, 2], [1, 3, 2], [2, 3, 1], [3, 2, 1]]

iex> 1..3 |> Combination.permutate(fn p -> Enum.at(p, 0) == 1 end)
[[1, 2, 3], [1, 3, 2]]