Pergunta

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

Foi útil?

Solução

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

Result = dict:merge(fun (_Key, V1, V2) -> V1 + V2 end, A, B)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top