[erlang-questions] Wanted additions to the maps module?

Michael Truog mjtruog@REDACTED
Tue May 3 03:02:01 CEST 2016

On 05/02/2016 08:59 AM, Jacob wrote:
> Hi,
> I just came across the point where I wished there was a merge/3 function
> like in dict/ordict:
>    merge(Fun, Map1, Map2) -> Map3

I agree that it is very helpful to have the same interface as the dict module (it is unfortunate that the maps module is unable to provide the same dict interface, since that makes Erlang data structure usage more complex than it needs to be, with no justification (I understand the intent was likely to discourage doing things that are inefficient)).  The maps merge function was discussed in the past at http://erlang.2086793.n4.nabble.com/Merging-maps-with-a-fun-like-dict-merge-3-td4713590.html with the thread preferring:

merge1(Fun, Map1, Map2) ->
     maps:merge(Map1, maps:map(fun(K, V2) ->
         case maps:find(K, Map1) of
             {ok, V1} ->
                 Fun(K, V1, V2);
             error ->
     end, Map2)).

The maps:merge/3 function can also be implemented using the new maps:update_with/4 function (in the git repo, from https://github.com/erlang/otp/pull/1025 ) which is based on the dict:merge/3 documentation (http://erlang.org/doc/man/dict.html#merge-3):

merge2(Fun, Map1, Map2) ->
     maps:fold(fun (K, V1, Map) ->
                   maps:update_with(K, fun (V2) -> Fun(K, V1, V2) end, V1, Map)
               end, Map2, Map1).

> Cheers
> Jacob
> On 04/08/2016 08:31 PM, Björn-Egil Dahlberg wrote:
>> Hi there!
>> I would like to know if you have any desires that we extend the current
>> maps module with additional functionality?
>> Have you repeated some code or built your own private lib to handle
>> certain maps specific tasks and thought "why isn't this in the maps module?"
>> I would like to know!
>> There is still time before 19.0 to extend the current API.
>> We have for example discussed adding {get, put, update,
>> remove}_path type of functions where we handle nested maps. Would this
>> be useful? Would it be used?
>> What have we missed?
>> // Björn-Egil
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

More information about the erlang-questions mailing list