[erlang-questions] adding maps:merge_nested/2

Karlo Kuna kuna.prime@REDACTED
Mon Nov 14 06:57:54 CET 2016

I'm actually working on something similar. And problem is that one cannot
assume that merge logic is the same for all nested levels. In other words
for M1{....Kn => M2 } (i am using incorrect syntax for clarity ) we cannot
assume that merging function is the same for M1 and M2. So then one can try
to pass list of merger functions that each correspond to "merging" level

M1{... Kn => M2 {...Knm => M3}}, [F1,F2,F3] where F1(M1) would
"recursively" call F2(M2) and so on

but this is also incorrect assumption and one can imagine that map can have
multiple nested maps on the same level with
 different semantics for merging as:

M1{...Kn=> M2 ... Km => M3}
where we want function F1 to merge M2 and F3 to merge M3.

there is also problem in type conversion:

M1{K => atom} , M2{K => M3}

should key K now be list: [atom, M3]?? It cannot be  map as there is no
valid key, except in odd cases where you want something like
Mr{K => M{atom => M3}}. It is "natural" that it should be list but again it
could be tuple!

For now i think this problem gets easier if we generalize it. Then question
is how to merge ANY two objects in erlang ? My answer is to pass a pair to
the merge function that is merge on type dispatch.

F(X, Y) when is_list(X) andalso is_list(Y) ->
           %get two elements Xn, Yn
           F(Xn, Yn),
F(X, Y) when is_atom(X) andalso is_atom(Y) -> // branch termination
           X; % this is just for illustration logic can be to throw or what

IMO this strategy gives most flexibility and one can make wrapper

merge(X, Y , F);

and then provide implementation of different merging logics to be used as

my_merge_logic(X, Y) .....

call merge

merge(X, Y, fun my_merge_logic/2),
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20161114/8bb5915f/attachment.htm>

More information about the erlang-questions mailing list