<div dir="ltr">Hi Vans,<div><br></div><div>Suppose we have</div><div><br></div><div>Map1 = #{ a => 1, b => 2 },</div><div>Map2 = #{ b => 3, c => 3 },</div><div><br></div><div>and we call maps:merge(F, Map1, Map2). F will be called 3 times with the following inputs:</div><div><br></div><div>F(a, {left, 1}) %% because a is only in the left side of the map.</div><div>F(b, {both, 2, 3}) %% since b is in both maps and the value 2 is in the left and 3 in the right</div><div>F(c, {right, 3}) %% since c is only in the right map</div><div><br></div><div>In each case, we can return a new value {ok, V} for some new value V, perhaps computed from the input. Or we can return undefined in the case we wish to have the new map ignore that key completely and omit it from the produced/merged map.</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Nov 15, 2016 at 4:23 PM Vans S <<a href="mailto:vans_163@yahoo.com">vans_163@yahoo.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">> fun<br class="gmail_msg">
> (K, {left, VL}) -> Res;<br class="gmail_msg">
> (K, {right, VR}) -> Res;<br class="gmail_msg">
> (K, {both, VL, VR}) -> Res<br class="gmail_msg">
> end<br class="gmail_msg">
<br class="gmail_msg">
Some questions on what VL/VR are once inside a nest, or does that happen all behind the scenes (going in nests). So you only need to compare the current values and return what will replace them?<br class="gmail_msg">
<br class="gmail_msg">
It seems like this is a more common use case then I initially thought. Having a general way to do this in erlang/OTP would be useful.<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
On Tuesday, November 15, 2016 7:54 AM, Jesper Louis Andersen <<a href="mailto:jesper.louis.andersen@gmail.com" class="gmail_msg" target="_blank">jesper.louis.andersen@gmail.com</a>> wrote:<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
On Mon, Nov 14, 2016 at 9:20 PM Michael Truog <<a href="mailto:mjtruog@gmail.com" class="gmail_msg" target="_blank">mjtruog@gmail.com</a>> wrote:<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
>The main merge function you are missing in the maps module, is the merge/3 (instead of merge/2) that is common in other modules (like the dict/orddict modules). I added an implementation of merge/3 at <a href="https://github.com/okeuday/mapsd" rel="noreferrer" class="gmail_msg" target="_blank">https://github.com/okeuday/mapsd</a> due to needing the dict API elsewhere. With the merge/3 function it should be easier to merge nested maps, in whatever way is required, since one size shouldn't fit all.<br class="gmail_msg">
><br class="gmail_msg">
<br class="gmail_msg">
In OCaml, you often have a merge-function like this one:<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
val merge : ('k, 'v1, 'cmp) t -> ('k, 'v2, 'cmp) t -> f:(key:'k -> [<br class="gmail_msg">
| `Left of 'v1<br class="gmail_msg">
| `Right of 'v2<br class="gmail_msg">
| `Both of 'v1 * 'v2<br class="gmail_msg">
] -> 'v3 option) -> ('k, 'v3, 'cmp) t<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
which means that you have to supply a function of the form<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
fun<br class="gmail_msg">
<br class="gmail_msg">
(K, {left, VL}) -> Res;<br class="gmail_msg">
<br class="gmail_msg">
(K, {right, VR}) -> Res;<br class="gmail_msg">
<br class="gmail_msg">
(K, {both, VL, VR}) -> Res<br class="gmail_msg">
<br class="gmail_msg">
end<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
where Res is either undefined | {ok, Result} for some result value. The semantics are that left,right, and both encodes on which side the value was in the input maps. And the Res encodes if a new value should be produced in the new map.<br class="gmail_msg">
<br class="gmail_msg">
_______________________________________________<br class="gmail_msg">
>erlang-questions mailing list<br class="gmail_msg">
><a href="mailto:erlang-questions@erlang.org" class="gmail_msg" target="_blank">erlang-questions@erlang.org</a><br class="gmail_msg">
><a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" class="gmail_msg" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br class="gmail_msg">
><br class="gmail_msg">
><br class="gmail_msg">
</blockquote></div>