Skip to content

Instantly share code, notes, and snippets.

@aratak
Last active February 24, 2016 20:10
Show Gist options
  • Save aratak/b230d75132d0bd203c00 to your computer and use it in GitHub Desktop.
Save aratak/b230d75132d0bd203c00 to your computer and use it in GitHub Desktop.
elixir implementation of array combination: http://ruby-doc.org/core-1.9.3/Array.html#method-i-combination
defmodule MyModule.ArrayCombination do
def combination(_, 0), do: [[]]
def combination(list, n) when (length(list) < n), do: []
def combination(list, n) when (length(list) == n), do: [list]
def combination(list, 1), do: Enum.map(list, fn(x)-> [x] end)
def combination([head|tail], n) do
Enum.map(combination(tail, n-1), fn(x)-> [head|x] end) ++ combination(tail, n)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment