<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body 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 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]) -><br>
        lists:foldl(fun(Y, X) -><br>
                            <a class="moz-txt-link-freetext" href="maps:merge(X">maps:merge(X</a>, <a class="moz-txt-link-freetext" href="maps:map(fun(Key">maps:map(fun(Key</a>, Value) -><br>
                                                           case
    <a class="moz-txt-link-freetext" href="maps:is_key(Key">maps:is_key(Key</a>, X) of<br>
                                                               false
    -><br>
                                                                  
    Value;<br>
                                                               true
    -><br>
                                                                  
    F(Key, Value, <a class="moz-txt-link-freetext" href="maps:get(Key">maps:get(Key</a>, X))<br>
                                                           end<br>
                                                   end,<br>
                                                   Y))<br>
                    end,<br>
                    Head,<br>
                    Tail).<br>
    <br>
    This has desired behaviour, and will probably still perform well. 
    Using lists:foldl is a brilliant idea.<br>
    <br>
    Best wishes<br>
    <br>
    Ivan<br>
    <br>
    <br>
    <div class="moz-cite-prefix">On 26/08/2015 09:16, Alexander
      Petrovsky wrote:<br>
    </div>
    <blockquote
cite="mid:CAH57y_RoFpPd8hc4191yf-FTDDUuni=W_i3bWnU3fuLH+cJ6uw@mail.gmail.com"
      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 class="moz-txt-link-freetext" href="maps:merge(X">maps:merge(X</a>,
          <a class="moz-txt-link-freetext" href="maps:map(fun(Key">maps:map(fun(Key</a>, Value) -> F(<a class="moz-txt-link-freetext" href="maps:get(Key">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"
              href="mailto:ivan@llaisdy.com" target="_blank">ivan@llaisdy.com</a>></span>:<br>
          <blockquote class="gmail_quote" style="margin:0 0 0
            .8ex;border-left:1px #ccc solid;padding-left:1ex">Dear All<br>
            <br>
            The maps module does not have a merge/3 like <a class="moz-txt-link-freetext" href="dict:merge/3">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 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 class="moz-txt-link-freetext" href="maps:fold(fun(K">maps:fold(fun(K</a>, V1, Acc) -><br>
                                  case <a class="moz-txt-link-freetext" href="maps:is_key(K">maps:is_key(K</a>, Acc) of<br>
                                      false -><br>
                                          <a class="moz-txt-link-freetext" href="maps:put(K">maps:put(K</a>, V1, Acc);<br>
                                      true -><br>
                                          V2 = <a class="moz-txt-link-freetext" href="maps:get(K">maps:get(K</a>, Acc),<br>
                                          <a class="moz-txt-link-freetext" href="maps:put(K">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"
              href="mailto:ivan@llaisdy.com" target="_blank">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"
              href="http://www.linkedin.com/in/ivanuemlianin"
              rel="noreferrer" target="_blank">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 class="gmail_signature">
          <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 class="moz-signature" cols="72">-- 
============================================================
Ivan A. Uemlianin PhD
Llaisdy
Speech Technology Research and Development

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

                        festina lente
============================================================ </pre>
  </body>
</html>