<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">A straight-forward way to provide a
      maps merge function based on the dict documentation is:<br>
      <br>
      maps_update(K, F, V0, Map) -><br>
          try maps:get(K, Map) of<br>
              V1 -><br>
                  maps:put(K, F(V1), Map)<br>
          catch<br>
              error:{badkey, K} -><br>
                  maps:put(K, V0, Map)<br>
          end.<br>
      <br>
      maps_merge(Fun, Map1, Map2) -><br>
          maps:fold(fun (K, V1, Map) -><br>
                        maps_update(K, fun (V2) -> Fun(K, V1, V2)
      end, V1, Map)<br>
                    end, Map2, Map1).<br>
      <br>
      Not sure about the speed compared to the code below, but both
      results should be the same if everything is working properly.<br>
      <br>
      On 08/26/2015 11:44 AM, Ivan Uemlianin wrote:<br>
    </div>
    <blockquote cite="mid:55DE091A.5090104@llaisdy.com" type="cite">
      <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
      Dear Alexander, Hynek<br>
      <br>
      That is a *lot* faster! :D <br>
      <br>
      Thanks very much both.<br>
      <br>
      Ivan<br>
      <br>
      <br>
      <div class="moz-cite-prefix">On 08/26/15 10:38, Ivan Uemlianin
        wrote:<br>
      </div>
      <blockquote cite="mid:55DD8933.10001@llaisdy.com" type="cite">
        <meta content="text/html; charset=UTF-8"
          http-equiv="Content-Type">
        Oh yes of course that's better.  Thanks<br>
        <br>
        Ivan<br>
        <br>
        <br>
        <div class="moz-cite-prefix">On 26/08/2015 10:32, Hynek Vychodil
          wrote:<br>
        </div>
        <blockquote
cite="mid:CAL_wnpdX2F9vtRd_YGjmMTscwhRcBD830+Mhfg+Zd-reYvKCSg@mail.gmail.com"
          type="cite">
          <div dir="ltr">
            <div>I would consider:</div>
            <div><br>
            </div>
            <div>merge_ap(F, [Head | Tail]) -></div>
            <div>    lists:foldl(fun(Y, X) -></div>
            <div>                        <a moz-do-not-send="true"
                class="moz-txt-link-freetext" href="maps:merge%28X">maps:merge(X</a>,
              <a moz-do-not-send="true" class="moz-txt-link-freetext"
                href="maps:map%28fun%28Key">maps:map(fun(Key</a>, Value)
              -></div>
            <div>                                                     
               case <a moz-do-not-send="true"
                class="moz-txt-link-freetext" href="maps:find%28Key">maps:find(Key</a>,
              X) of</div>
            <div>                                                       
                 {ok, V} -> F(Key, V, Value);</div>
            <div>                                                       
                 error    -> Value</div>
            <div>                                                     
               end<br>
            </div>
            <div>                                               end,</div>
            <div>                                               Y))</div>
            <div>                end,</div>
            <div>                Head,</div>
            <div>                Tail).</div>
            <div class="gmail_extra"><br>
              <div class="gmail_quote">On Wed, Aug 26, 2015 at 10:45 AM,
                Ivan Uemlianin <span dir="ltr"><<a
                    moz-do-not-send="true"
                    class="moz-txt-link-abbreviated"
                    href="mailto:ivan@llaisdy.com">ivan@llaisdy.com</a>></span>
                wrote:<br>
                <blockquote class="gmail_quote" style="margin:0px 0px
                  0px
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
                  <div bgcolor="#FFFFFF" text="#000000"> Dear Alexander<br>
                    <br>
                    Thanks!  That looks excellent!<br>
                    <br>
                    I notice that your function F is different from the
                    <a moz-do-not-send="true"
                      class="moz-txt-link-freetext" href="dict:merge/3">dict:merge/3</a>
                    function: instead of taking a key and the two
                    values, is takes two values, defaulting the first to
                    0.  <br>
                    <br>
                    I've put my case clause into your framework:<br>
                    <br>
                    merge_ap(F, [Head | Tail]) -><span class=""><br>
                          lists:foldl(fun(Y, X) -><br>
                                              <a moz-do-not-send="true">maps:merge(X</a>,
                      <a moz-do-not-send="true">maps:map(fun(Key</a>,
                      Value) -><br>
                    </span>
                                                                          
                    case <a moz-do-not-send="true">maps:is_key(Key</a>,
                    X) of<br>
                                                                              


                    false -><br>
                                                                                  



                    Value;<br>
                                                                              


                    true -><br>
                                                                                  



                    F(Key, Value, <a moz-do-not-send="true">maps:get(Key</a>,
                    X))<br>
                                                                          

                    end<span class=""><br>
                                                                    
                      end,<br>
                                                                     Y))<br>
                                      end,<br>
                                      Head,<br>
                                      Tail).<br>
                      <br>
                    </span> This has desired behaviour, and will
                    probably still perform well.  Using lists:foldl is a
                    brilliant idea.<br>
                    <br>
                    Best wishes<span class=""><font color="#888888"><br>
                        <br>
                        Ivan</font></span>
                    <div>
                      <div class="h5"><br>
                        <br>
                        <br>
                        <div>On 26/08/2015 09:16, Alexander Petrovsky
                          wrote:<br>
                        </div>
                        <blockquote type="cite">
                          <div dir="ltr">Hi!
                            <div><br>
                            </div>
                            <div>I've make some little benchmarks, and
                              noticed that the most effective way is:</div>
                            <div><br>
                            </div>
                            <div>merge(F, [Head | Tail]) -></div>
                            <div>    lists:foldl(fun(Y, X) -> <a
                                moz-do-not-send="true"
                                class="moz-txt-link-freetext"
                                href="maps:merge%28X">maps:merge(X</a>,
                              <a moz-do-not-send="true"
                                class="moz-txt-link-freetext"
                                href="maps:map%28fun%28Key">maps:map(fun(Key</a>,
                              Value) -> F(<a moz-do-not-send="true">maps:get(Key</a>,
                              X, 0), Value) end, Y)) end, Head, Tail).</div>
                            <div><br>
                            </div>
                          </div>
                          <div class="gmail_extra"><br>
                            <div class="gmail_quote">2015-08-23 20:05
                              GMT+03:00 Ivan Uemlianin <span dir="ltr"><<a
                                  moz-do-not-send="true"
                                  class="moz-txt-link-abbreviated"
                                  href="mailto:ivan@llaisdy.com">ivan@llaisdy.com</a>></span>:<br>
                              <blockquote class="gmail_quote"
                                style="margin:0px 0px 0px
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Dear


                                All<br>
                                <br>
                                The maps module does not have a merge/3
                                like <a moz-do-not-send="true">dict:merge/3</a>,
                                which calls a function fun(Key, Val1,
                                Val2) when both dicts have the same
                                key.  From the documentation, "If a key
                                occurs in both dictionaries then Fun is
                                called with the key and both values to
                                return a new value."<br>
                                <br>
                                The dict documentation says that <a
                                  moz-do-not-send="true"
                                  class="moz-txt-link-freetext"
                                  href="dict:merge/3">dict:merge/3</a>
                                is faster than an equivalent implemented
                                in erlang.<br>
                                <br>
                                <a moz-do-not-send="true"
                                  href="http://www.erlang.org/doc/man/dict.html#merge-3"
                                  rel="noreferrer" target="_blank">http://www.erlang.org/doc/man/dict.html#merge-3</a><br>
                                <br>
                                Why does the maps module not have a
                                similar merge/3?  Is it because writing
                                our own erlang implementation is at fast
                                as it gets?<br>
                                <br>
                                Below is an implementation which seems
                                to work.  Can it be made faster?<br>
                                <br>
                                <br>
                                merge_maps(Fun, M1, M2) -><br>
                                    <a moz-do-not-send="true">maps:fold(fun(K</a>,
                                V1, Acc) -><br>
                                                      case <a
                                  moz-do-not-send="true"
                                  class="moz-txt-link-freetext"
                                  href="maps:is_key%28K">maps:is_key(K</a>,
                                Acc) of<br>
                                                          false -><br>
                                                              <a
                                  moz-do-not-send="true"
                                  class="moz-txt-link-freetext"
                                  href="maps:put%28K">maps:put(K</a>,
                                V1, Acc);<br>
                                                          true -><br>
                                                              V2 = <a
                                  moz-do-not-send="true"
                                  class="moz-txt-link-freetext"
                                  href="maps:get%28K">maps:get(K</a>,
                                Acc),<br>
                                                              <a
                                  moz-do-not-send="true"
                                  class="moz-txt-link-freetext"
                                  href="maps:put%28K">maps:put(K</a>,
                                Fun(K, V1, V2), Acc)<br>
                                                      end<br>
                                              end,<br>
                                              M1,<br>
                                              M2).<br>
                                <br>
                                merge_maps_test() -><br>
                                    merge_maps(fun(_K, V1, V2) -> V1
                                + V2 end,<br>
                                               #{a => 1, b => 2, c
                                => 3},<br>
                                               #{b => 2, c => 3, d
                                => 4}) =:=<br>
                                        #{a => 1, b => 4, c =>
                                6, d => 4}.<br>
                                <br>
                                <br>
                                With thanks and best wishes<br>
                                <br>
                                Ivan<br>
                                <br>
                                -- <br>
============================================================<br>
                                Ivan A. Uemlianin PhD<br>
                                Llaisdy<br>
                                Speech Technology Research and
                                Development<br>
                                <br>
                                                    <a
                                  moz-do-not-send="true"
                                  class="moz-txt-link-abbreviated"
                                  href="mailto:ivan@llaisdy.com">ivan@llaisdy.com</a><br>
                                                        @llaisdy<br>
                                                         <a
                                  moz-do-not-send="true"
                                  href="http://llaisdy.wordpress.com"
                                  rel="noreferrer" target="_blank">llaisdy.wordpress.com</a><br>
                                              <a moz-do-not-send="true"
                                  href="http://github.com/llaisdy"
                                  rel="noreferrer" target="_blank">github.com/llaisdy</a><br>
                                                     <a
                                  moz-do-not-send="true"
                                  class="moz-txt-link-abbreviated"
                                  href="http://www.linkedin.com/in/ivanuemlianin">www.linkedin.com/in/ivanuemlianin</a><br>
                                <br>
                                                        festina lente<br>
============================================================<br>
                                <br>
_______________________________________________<br>
                                erlang-questions mailing list<br>
                                <a moz-do-not-send="true"
                                  href="mailto:erlang-questions@erlang.org"
                                  target="_blank">erlang-questions@erlang.org</a><br>
                                <a moz-do-not-send="true"
                                  href="http://erlang.org/mailman/listinfo/erlang-questions"
                                  rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
                              </blockquote>
                            </div>
                            <br>
                            <br clear="all">
                            <div><br>
                            </div>
                            -- <br>
                            <div>
                              <div dir="ltr">Петровский Александр /
                                Alexander Petrovsky,<br>
                                <br>
                                Skype: askjuise<br>
                                <div>Phone: +7 914 8 820 815
                                  <div><br>
                                  </div>
                                </div>
                              </div>
                            </div>
                          </div>
                        </blockquote>
                        <br>
                        <pre cols="72">-- 
============================================================
Ivan A. Uemlianin PhD
Llaisdy
Speech Technology Research and Development

                    <a moz-do-not-send="true" href="mailto:ivan@llaisdy.com" target="_blank">ivan@llaisdy.com</a>
                        @llaisdy
                         <a moz-do-not-send="true" href="http://llaisdy.wordpress.com" target="_blank">llaisdy.wordpress.com</a>
              <a moz-do-not-send="true" href="http://github.com/llaisdy" target="_blank">github.com/llaisdy</a>
                     <a moz-do-not-send="true" href="http://www.linkedin.com/in/ivanuemlianin" target="_blank">www.linkedin.com/in/ivanuemlianin</a>

                        festina lente
============================================================ </pre>
                      </div>
                    </div>
                  </div>
                  <br>
                  _______________________________________________<br>
                  erlang-questions mailing list<br>
                  <a moz-do-not-send="true"
                    href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
                  <a moz-do-not-send="true"
                    href="http://erlang.org/mailman/listinfo/erlang-questions"
                    rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
                  <br>
                </blockquote>
              </div>
              <br>
            </div>
          </div>
        </blockquote>
        <br>
        <pre class="moz-signature" cols="72">-- 
============================================================
Ivan A. Uemlianin PhD
Llaisdy
Speech Technology Research and Development

                    <a moz-do-not-send="true" class="moz-txt-link-abbreviated" href="mailto:ivan@llaisdy.com">ivan@llaisdy.com</a>
                        @llaisdy
                         llaisdy.wordpress.com
              github.com/llaisdy
                     <a moz-do-not-send="true" class="moz-txt-link-abbreviated" href="http://www.linkedin.com/in/ivanuemlianin">www.linkedin.com/in/ivanuemlianin</a>

                        festina lente
============================================================ </pre>
        <br>
        <fieldset class="mimeAttachmentHeader"></fieldset>
        <br>
        <pre wrap="">_______________________________________________
erlang-questions mailing list
<a moz-do-not-send="true" class="moz-txt-link-abbreviated" href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a>
<a moz-do-not-send="true" class="moz-txt-link-freetext" href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a>
</pre>
      </blockquote>
      <br>
      <pre class="moz-signature" cols="72">-- 
============================================================
Ivan A. Uemlianin PhD
Llaisdy
Speech Technology Research and Development

                    <a moz-do-not-send="true" class="moz-txt-link-abbreviated" href="mailto:ivan@llaisdy.com">ivan@llaisdy.com</a>
                        @llaisdy
                         llaisdy.wordpress.com
              github.com/llaisdy
                     <a moz-do-not-send="true" class="moz-txt-link-abbreviated" href="http://www.linkedin.com/in/ivanuemlianin">www.linkedin.com/in/ivanuemlianin</a>

                        festina lente
============================================================ 

</pre>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
erlang-questions mailing list
<a class="moz-txt-link-abbreviated" href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a>
<a class="moz-txt-link-freetext" href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>