문제

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

도움이 되었습니까?

해결책

Assume your dicts are bound to A and B variables respectevily, then:

Result = dict:merge(fun (_Key, V1, V2) -> V1 + V2 end, A, B)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top