Last active
February 24, 2016 20:10
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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