Erlang histogram summing 2 maps
-
15-06-2021 - |
Question
How do you create a histogram sum of two dicts in erlang? Example if you have 2 dicts:
{a: 1, c: 4, d:7} and {a:5, b:8, d:1}
The sum would be:
{a:6, b:8, c:4, d:8}
Thanks
Solution
Assume your dicts are bound to A
and B
variables respectevily, then:
Result = dict:merge(fun (_Key, V1, V2) -> V1 + V2 end, A, B)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow