<div dir="ltr">Hi!<div><br></div><div>I've make some little benchmarks, and noticed that the most effective way is:</div><div><br></div><div>merge(F, [Head | Tail]) -></div><div> lists:foldl(fun(Y, X) -> maps:merge(X, maps:map(fun(Key, Value) -> F(maps:get(Key, X, 0), Value) end, Y)) end, Head, Tail).</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">2015-08-23 20:05 GMT+03:00 Ivan Uemlianin <span dir="ltr"><<a href="mailto:ivan@llaisdy.com" target="_blank">ivan@llaisdy.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Dear All<br>
<br>
The maps module does not have a merge/3 like dict:merge/3, which calls a function fun(Key, Val1, Val2) when both dicts have the same key. From the documentation, "If a key occurs in both dictionaries then Fun is called with the key and both values to return a new value."<br>
<br>
The dict documentation says that dict:merge/3 is faster than an equivalent implemented in erlang.<br>
<br>
<a href="http://www.erlang.org/doc/man/dict.html#merge-3" rel="noreferrer" target="_blank">http://www.erlang.org/doc/man/dict.html#merge-3</a><br>
<br>
Why does the maps module not have a similar merge/3? Is it because writing our own erlang implementation is at fast as it gets?<br>
<br>
Below is an implementation which seems to work. Can it be made faster?<br>
<br>
<br>
merge_maps(Fun, M1, M2) -><br>
maps:fold(fun(K, V1, Acc) -><br>
case maps:is_key(K, Acc) of<br>
false -><br>
maps:put(K, V1, Acc);<br>
true -><br>
V2 = maps:get(K, Acc),<br>
maps:put(K, Fun(K, V1, V2), Acc)<br>
end<br>
end,<br>
M1,<br>
M2).<br>
<br>
merge_maps_test() -><br>
merge_maps(fun(_K, V1, V2) -> V1 + V2 end,<br>
#{a => 1, b => 2, c => 3},<br>
#{b => 2, c => 3, d => 4}) =:=<br>
#{a => 1, b => 4, c => 6, d => 4}.<br>
<br>
<br>
With thanks and best wishes<br>
<br>
Ivan<br>
<br>
-- <br>
============================================================<br>
Ivan A. Uemlianin PhD<br>
Llaisdy<br>
Speech Technology Research and Development<br>
<br>
<a href="mailto:ivan@llaisdy.com" target="_blank">ivan@llaisdy.com</a><br>
@llaisdy<br>
<a href="http://llaisdy.wordpress.com" rel="noreferrer" target="_blank">llaisdy.wordpress.com</a><br>
<a href="http://github.com/llaisdy" rel="noreferrer" target="_blank">github.com/llaisdy</a><br>
<a href="http://www.linkedin.com/in/ivanuemlianin" rel="noreferrer" target="_blank">www.linkedin.com/in/ivanuemlianin</a><br>
<br>
festina lente<br>
============================================================<br>
<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">Петровский Александр / Alexander Petrovsky,<br><br>Skype: askjuise<br><div>Phone: +7 914 8 820 815<div><br></div></div></div></div>
</div>