<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>                        maps:merge(X, maps:map(fun(Key, Value) -></div><div>                                                       case maps:find(Key, 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 href="mailto:ivan@llaisdy.com" target="_blank">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>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>maps:merge(X</a>, <a>maps:map(fun(Key</a>, Value) -><br></span>
                                                           case
    <a>maps:is_key(Key</a>, X) of<br>
                                                               false
    -><br>
                                                                  
    Value;<br>
                                                               true
    -><br>
                                                                  
    F(Key, Value, <a>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>maps:merge(X</a>,
          <a>maps:map(fun(Key</a>, Value) -> F(<a>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 href="mailto:ivan@llaisdy.com" target="_blank">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>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>dict:merge/3</a> is faster than
            an equivalent implemented in erlang.<br>
            <br>
            <a 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>maps:fold(fun(K</a>, V1, Acc) -><br>
                                  case <a>maps:is_key(K</a>, Acc) of<br>
                                      false -><br>
                                          <a>maps:put(K</a>, V1, Acc);<br>
                                      true -><br>
                                          V2 = <a>maps:get(K</a>, Acc),<br>
                                          <a>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 href="mailto:ivan@llaisdy.com" target="_blank">ivan@llaisdy.com</a><br>
                                    @llaisdy<br>
                                     <a href="http://llaisdy.wordpress.com" rel="noreferrer" target="_blank">llaisdy.wordpress.com</a><br>
                          <a href="http://github.com/llaisdy" rel="noreferrer" target="_blank">github.com/llaisdy</a><br>
                                 <a 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 href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
            <a 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 href="mailto:ivan@llaisdy.com" target="_blank">ivan@llaisdy.com</a>
                        @llaisdy
                         <a href="http://llaisdy.wordpress.com" target="_blank">llaisdy.wordpress.com</a>
              <a href="http://github.com/llaisdy" target="_blank">github.com/llaisdy</a>
                     <a 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 href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a 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>