Hi,
I have a recuring pattern that I feel could be done better. There's a list of 2-tuples where the tuples are {key, value} pairs. The same key can be there more than once in the list. I want to transform it into a map where the keys are the keys from the tuples and the values become lists of all the values coming from the tuples with the same key. (The order in the list doesn't matter.)
For examle:
list_of_tuples = [{1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}, {5, 6}]
--> %{1 => [2, 3, 4], 2 => [3, 4], 3 => [4], 5 => [6]}
I used to do this with the following transformation:
list_of_tuples
|> Enum.group_by(&(elem(&1,0)))
|> Enum.map(fn {k, v} -> {k, Enum.map(v, &(elem(&1, 1)))} end)
|> Enum.into(%{})
Is there any better or more elegant way?