combinatorics v0.1.0 Combinatorics View Source

Utility for generating combinatorics. Extracted from the implementation in CouchDB.

Link to this section Summary

Functions

Generate all combinations of true or false for a given number of bits

Generate n number of combinations of items in given list

Generate all permutations of a given list of lists

Generate a powerset for a given list

Generate all product a given list

Link to this section Functions

Link to this function

binary_combinations(n) View Source
binary_combinations(pos_integer()) :: [[boolean()]]

Generate all combinations of true or false for a given number of bits.

Returns a list of lists.

Examples

iex> Combinatorics.binary_combinations(3)
[
   [ false , false , false ],
   [ false , false , true  ],
   [ false , true  , false ],
   [ false , true  , true  ],
   [ true  , false , false ],
   [ true  , false , true  ],
   [ true  , true  , false ],
   [ true  , true  , true  ]
]
Link to this function

n_combinations(n, list) View Source
n_combinations(pos_integer(), list()) :: [list()]

Generate n number of combinations of items in given list.

Returns a list of lists with n element.

Examples

iex> Combinatorics.n_combinations(2, [:mon, :tue, :wed, :thu, :fri])
[
  [:mon, :tue],
  [:mon, :wed],
  [:mon, :thu],
  [:mon, :fri],
  [:tue, :wed],
  [:tue, :thu],
  [:tue, :fri],
  [:wed, :thu],
  [:wed, :fri],
  [:thu, :fri]
]
Link to this function

permutations(list) View Source
permutations(list()) :: list()

Generate all permutations of a given list of lists.

Returns a list of lists.

Examples

iex> Combinatorics.permutations([:foo, :bar, :baz])
[
  [:foo, :bar, :baz],
  [:foo, :baz, :bar],
  [:bar, :foo, :baz],
  [:bar, :baz, :foo],
  [:baz, :foo, :bar],
  [:baz, :bar, :foo]
]
Link to this function

powerset(list) View Source
powerset(list()) :: [list()]

Generate a powerset for a given list.

Returns a list.

Examples

iex> Combinatorics.powerset([:foo, :bar, :baz])
[
    [:foo],
    [:foo,:baz],
    [:foo,:bar,:baz],
    [:foo,:bar],
    [:bar],
    [:bar,:baz],
    [:baz],
    []
]
Link to this function

product(list) View Source
product([list()]) :: [list()]

Generate all product a given list.

Returns a list.

Examples

iex> Combinatorics.product([ [:foo, :bar], [1, 2, 3] ])
[
  [:foo, 1],
  [:foo, 2],
  [:foo, 3],
  [:bar, 1],
  [:bar, 2],
  [:bar, 3]
]