I\'ve been tinkering with Elixir for the last few weeks. I just came across this succinct combinations algorithm in Erlang, which I tried rewriting in Elixir but got stuck.<
You need to wrap the for expression in parentheses like the Erlang code.
def combination(n, [x|xs]) do
(for y <- combination(n - 1, xs), do: [x|y]) ++ combination(n, xs)
end
Demo:
iex(1)> defmodule Foo do
...(1)> def combination(0, _), do: [[]]
...(1)> def combination(_, []), do: []
...(1)> def combination(n, [x|xs]) do
...(1)> (for y <- combination(n - 1, xs), do: [x|y]) ++ combination(n, xs)
...(1)> end
...(1)> end
{:module, Foo,
<<70, 79, 82, 49, 0, 0, 6, 100, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 137, 131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95, 118, 49, 108, 0, 0, 0, 2, 104, 2, ...>>,
{:combination, 2}}
iex(2)> Foo.combination 2, [1, 2, 3]
[[1, 2], [1, 3], [2, 3]]