Skip to content

Instantly share code, notes, and snippets.

@alco
Last active December 29, 2015 03:19
Show Gist options
  • Select an option

  • Save alco/7607273 to your computer and use it in GitHub Desktop.

Select an option

Save alco/7607273 to your computer and use it in GitHub Desktop.
defmodule P do
def product([]) do
[]
end
def product(lists) do
product(lists, [])
end
def product([h|t], args) do
Enum.map(h, fn x ->
product(t, [x | args])
end) |> Enum.concat
end
def product([], args) do
[list_to_tuple(Enum.reverse(args))]
end
end
iex(7)> P.product([[0, 1]])
[{0}, {1}]
iex(8)> P.product([[0, 1], [:a, :b]])
[{0, :a}, {0, :b}, {1, :a}, {1, :b}]
iex(9)> P.product([[0, 1], [:a, :b], ['x', 'y']])
[{0, :a, 'x'}, {0, :a, 'y'}, {0, :b, 'x'}, {0, :b, 'y'}, {1, :a, 'x'},
{1, :a, 'y'}, {1, :b, 'x'}, {1, :b, 'y'}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment