<html><head></head><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px"><div id="yui_3_16_0_ym19_1_1478446594473_782486" dir="ltr"><span id="yui_3_16_0_ym19_1_1478446594473_782485">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<br>merge({#{}, #{}}, {#{},#{5=>6}}) and giving {#{}, #{5=>6}}.<br><br>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.<br><br>If a general function is used, where would it reside?<br><br>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.<br></span></div> <div class="qtdSeparateBR"><br><br></div><div class="yahoo_quoted" style="display: block;"> <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;"> <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;"> <div dir="ltr"><font size="2" face="Arial"> On Monday, November 14, 2016 12:57 AM, Karlo Kuna <kuna.prime@gmail.com> wrote:<br></font></div> <br><br> <div class="y_msg_container"><div id="yiv0777199452"><div><div dir="ltr">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: <div><br clear="none"></div><div>M1{... Kn => M2 {...Knm => M3}}, [F1,F2,F3] where F1(M1) would "recursively" call F2(M2) and so on</div><div><br clear="none"></div><div>but this is also incorrect assumption and one can imagine that map can have multiple nested maps on the same level with</div><div> different semantics for merging as:</div><div><br clear="none"></div><div>M1{...Kn=> M2 ... Km => M3} </div><div>where we want function F1 to merge M2 and F3 to merge M3.</div><div><br clear="none"></div><div>there is also problem in type conversion: </div><div><br clear="none"></div><div>M1{K => atom} , M2{K => M3} </div><div><br clear="none"></div><div>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 </div><div>Mr{K => M{atom => M3}}. It is "natural" that it should be list but again it could be tuple! </div><div><br clear="none"></div><div>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. </div><div><br clear="none"></div><div>F(X, Y) when is_list(X) andalso is_list(Y) -> </div><div> %get two elements Xn, Yn</div><div> F(Xn, Yn),</div><div> ....;</div><div>F(X, Y) when is_atom(X) andalso is_atom(Y) -> // branch termination</div><div> X; % this is just for illustration logic can be to throw or what ever </div><div>.....</div><div> </div><div>IMO this strategy gives most flexibility and one can make wrapper </div><div><br clear="none"></div><div>merge(X, Y , F);</div><div><br clear="none"></div><div>and then provide implementation of different merging logics to be used as needed </div><div><br clear="none"></div><div>my_merge_logic(X, Y) .....</div><div><br clear="none"></div><div>call merge </div><div><br clear="none"></div><div>merge(X, Y, fun my_merge_logic/2),</div><div class="yiv0777199452yqt9061740632" id="yiv0777199452yqtfd31881"><div><br clear="none"></div><div><br clear="none"></div><div><br clear="none"></div><div class="yiv0777199452gmail_extra"><br clear="none"></div></div></div></div></div><br><br></div> </div> </div> </div></div></body></html>