2
votes

How can I convert this list in elixir

[
%{key1: 1, key2: 2, key3: 3},
%{key1: 4, key2: 5, key3: 6},
%{key1: 7, key2: 8, key3: 9}
]

to this map

%{key1: [1, 4, 7], key2: [2, 5, 8], key3: [3, 6, 9])

?

3

3 Answers

4
votes
Enum.reduce(input, %{key1: [], key2: [], key3: []}, fn m, acc ->
  Map.merge(acc, m, fn _k, v1, v2 -> v1 ++ [v2] end)
end)
#⇒ %{key1: [1, 4, 7], key2: [2, 5, 8], key3: [3, 6, 9]}

or, without preliminar hardcoded accumulator:

Enum.reduce(input, %{}, fn m, acc ->
  Map.merge(acc, m, fn
    _k, v1, v2 when is_list(v1) ->
      :lists.reverse([v2 | :lists.reverse(v1)])
    _k, v1, v2 -> [v1, v2]
  end)
end)
#⇒ %{key1: [1, 4, 7], key2: [2, 5, 8], key3: [3, 6, 9]}
8
votes

You can do this in two passes; The first flattens everything into a single one-dimensional list of key-value pairs, then the second pass groups by the keys.

input = [
  %{key1: 1, key2: 2, key3: 3},
  %{key1: 4, key2: 5, key3: 6},
  %{key1: 7, key2: 8, key3: 9}
]

input
|> Enum.flat_map(fn m -> Map.to_list(m) end) 
|> Enum.group_by(fn {k, _} -> k end, fn {_, v} -> v end) 

Output

%{key1: [1, 4, 7], key2: [2, 5, 8], key3: [3, 6, 9]}
0
votes

Not very good at Elixir yet, but I've come up with this:

map = Enum.reduce(list, %{}, fn(inner_map, acc) ->
        Map.merge(acc, inner_map, fn(_, v1, v2) ->
          if is_list(v1) do
            v1 ++ [v2]
          else
            [v1] ++ [v2]
          end
        end)
      end)

but I think there must be a more elegant way to do it