I have 2 lists of maps:
list1 =
[
%{amount: 1, id: 112006},
%{amount: 1, id: 78798},
%{amount: 6, id: 92572},
%{amount: 1, id: 89750},
%{amount: 1, id: 81418},
%{amount: 3, id: 92062},
%{amount: 1, id: 82373},
%{amount: 1, id: 92856}...
]
and
list2 =
[
%{count: 5, id: [112006, 92062, 92856, 67812, 70736], name: "Object 1"},
%{count: 655, id: [92572, 22432, 32368, 34180, 34181, 34182, ...], name: "Object 2"},
%{count: 238, id: [26052, 30430, 37067, 37068, 41228, 42686, ...], name: "Object 3"}
...
]
list1 is with 30000+ maps in it and list2 with about a 100 maps, the id's are the same in both list, I want to concat the two list into one:
[
%{count: 5, all_count: 5 name: "Object 1"},
%{count: 655, all_count: 3, name: "Object 2"},
....
]
With the new all_count-key that is the sum of all amount from list1 with the same id's that is in the id-array in list2.
I did:
Enum.map(list2, fn(map) ->
all_count =
list1
|> Enum.filter(&Enum.member?(map.id, &1.id))
|> Enum.map(&(&1.amount))
|> Enum.sum
Map.put(map, :all_count, all_count)
end)
witch works but is very slow and I need something faster, tried with Flow:
Enum.map(list2, fn(map) ->
all_count =
list1
|> Flow.from_enumerable()
|> Flow.filter(&Enum.member?(map.id, &1.id))
|> Flow.map(&(&1.amount))
|> Enum.sum
Map.put(map, :all_count, all_count)
end)
got it a bit quicker but not much, any tips how to get it faster? Tia.