View Source Formulae.Combinators.Stream (formulae v0.17.1)

Helper to calculate all the combinations / permutations of the enumerable given.

Similar to Formulae.Combinators but returns a stream.

Examples

iex> ~w|a b c d|a
...> |> Formulae.Combinators.Stream.combinations(2)
...> |> elem(0)
...> |> Enum.to_list()
[[:a, :b], [:a, :c], [:a, :d], [:b, :c], [:b, :d], [:c, :d]]
iex> ~w|a b c d|a
...> |> Formulae.Combinators.Stream.permutations(2)
...> |> elem(0)
...> |> Enum.to_list()
[[: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> ~w|a b c d|a
...> |> Formulae.Combinators.Stream.repeated_permutations(2)
...> |> elem(0)
...> |> Enum.to_list()
[[: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]]

iex> (for c <- ?a..?z, do: <<c>>)
...> |> Formulae.Combinators.Stream.combinations(12)
...> |> elem(0)
...> |> Stream.take_every(26)
...> |> Enum.take(2)
[["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"],
 ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "l", "x"]]
iex> l = for c <- ?a..?z, do: <<c>>
iex> with {stream, :ok} <- Formulae.Combinators.Stream.permutations(l, 12),
...>   do: stream |> Stream.take_every(26) |> Enum.take(2)
[["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"],
 ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "l", "w"]]
iex> (for c <- ?a..?z, do: <<c>>)
...> |> Formulae.Combinators.Stream.repeated_permutations(12)
...> |> elem(0)
...> |> Enum.take(5)
[["a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"],
 ["a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "b"],
 ["a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "c"],
 ["a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "d"],
 ["a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "e"]]

Summary

Functions

Lazily calculates combinations of the list, given as the first parameter, returns {%Stream{}, :ok} tuple.

Lazily calculates permutations of the list, given as the first parameter, returns {%Stream{}, :ok} tuple.

Lazily calculates repeated permutations of the list, given as the first parameter, returns {%Stream{}, :ok} tuple.

Functions

combinations(l, n)

(macro)
@spec combinations(list :: list(), count :: non_neg_integer()) :: {stream(), :ok}

Lazily calculates combinations of the list, given as the first parameter, returns {%Stream{}, :ok} tuple.

See Formulae.Combinators.combinations/2 for greedy version.

permutations(l, n)

(macro)
@spec permutations(list :: list(), count :: non_neg_integer()) :: {stream(), :ok}

Lazily calculates permutations of the list, given as the first parameter, returns {%Stream{}, :ok} tuple.

See Formulae.Combinators.permutations/2 for greedy version.

repeated_permutations(l, n)

(macro)
@spec repeated_permutations(list :: list(), count :: non_neg_integer()) ::
  {stream(), :ok}

Lazily calculates repeated permutations of the list, given as the first parameter, returns {%Stream{}, :ok} tuple.

See Formulae.Combinators.repeated_permutations/2 for greedy version.