This took me way too long and I think its right/working, but … am I missing a better solution?
defmodule IntersectingSets do
def meld([], [], [], result) do
result
end
def meld([item|rest], [], [], result) do
meld(rest, [item], result, [])
end
def meld(rest, [item], [], result) do
meld(rest, [], [], [item|result])
end
def meld(rest, [item], [head|tail], result) do
cond do
MapSet.intersection(item, head) |> MapSet.size == 0 ->
meld(rest, [item], tail, [head|result])
true ->
meld(rest, [MapSet.union(item, head)], tail, result)
end
end
def meld([item|rest]) do
meld(rest, [], [], [item])
end
end