[erlang-questions] adding maps:merge_nested/2

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Tue Nov 15 13:53:54 CET 2016


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
<https://ocaml.janestreet.com/ocaml-core/113.33/doc/core/Std/Map.mod/#/t.typ>
-> ('k, 'v2, 'cmp) t
<https://ocaml.janestreet.com/ocaml-core/113.33/doc/core/Std/Map.mod/#/t.typ>
-> f:(key:'k -> [
| `Left of 'v1
| `Right of 'v2
| `Both of 'v1 * 'v2
] -> 'v3 option) -> ('k, 'v3, 'cmp) t
<https://ocaml.janestreet.com/ocaml-core/113.33/doc/core/Std/Map.mod/#/t.typ>

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

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

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
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20161115/7c01a3eb/attachment.htm>


More information about the erlang-questions mailing list