[erlang-questions] adding maps:merge_nested/2

Vans S vans_163@REDACTED
Mon Nov 14 15:31:23 CET 2016

A general merge two terms function would definitely be both useful and complex, because questions arise where do you draw the line, do you just merge primitive types, or do you want to be able to merge proplists/orddicts/etc as well for example.  The only primitive complex types are tuples, lists and maps from the top of my head.  I can see this merge for example doing for a tuple
merge({#{}, #{}}, {#{},#{5=>6}}) and giving {#{}, #{5=>6}}.

A question arises if tuples, lists and maps should get their own merge functions then.  Lists have a merge function that you can implement yourself, maps do not. Tuples do not.

If a general function is used, where would it reside?

Merging the same key with one being atom and one being M3 must give you only M3.  But this custom logic such as create a list on merging same key with different atom value definitely has its uses, clojure has a library for stuff like this but I cant remember the name.

    On Monday, November 14, 2016 12:57 AM, Karlo Kuna <kuna.prime@REDACTED> wrote:

 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 as: 
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 ever ..... 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 needed 
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/6c122b63/attachment.htm>

More information about the erlang-questions mailing list