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

Was it helpful?

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
scroll top