Skip to content

Instantly share code, notes, and snippets.

@Glutexo
Created June 13, 2022 13:21
Show Gist options
  • Save Glutexo/65b8fa03801a1d7cb4bfb4a219774119 to your computer and use it in GitHub Desktop.
Save Glutexo/65b8fa03801a1d7cb4bfb4a219774119 to your computer and use it in GitHub Desktop.
Recursive combinations with subsets
defmodule Combination do
def combine([]) do
[]
end
def combine([head | tail]) do
tail_combinations = combine(tail)
merged_combinations = Enum.map(
[[]] ++ tail_combinations,
fn c -> c ++ [head] end
)
tail_combinations ++ merged_combinations
end
end
input = ["a", "b", "c"]
Combination.combine(input)
|> IO.inspect(limit: :infinity)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment