[erlang-questions] adding maps:merge_nested/2

Vans S vans_163@REDACTED
Tue Nov 15 16:22:18 CET 2016

> fun
>  (K, {left, VL}) -> Res;
>  (K, {right, VR}) -> Res;
>  (K, {both, VL, VR}) -> Res
> end

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?

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.

On Tuesday, November 15, 2016 7:54 AM, Jesper Louis Andersen <jesper.louis.andersen@REDACTED> wrote:

On Mon, Nov 14, 2016 at 9:20 PM Michael Truog <mjtruog@REDACTED> wrote:

>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 https://github.com/okeuday/mapsd 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.

In OCaml, you often have a merge-function like this one:

val merge : ('k, 'v1, 'cmp) t -> ('k, 'v2, 'cmp) t -> f:(key:'k -> [ 
| `Left of 'v1 
| `Right of 'v2 
| `Both of 'v1 * 'v2 
] -> 'v3 option) -> ('k, 'v3, 'cmp) t

which means that you have to supply a function of the form


  (K, {left, VL}) -> Res;

  (K, {right, VR}) -> Res;

  (K, {both, VL, VR}) -> Res


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.

>erlang-questions mailing list

More information about the erlang-questions mailing list