[erlang-questions] optimal way to append an element in a list inside a map

Caragea Silviu silviu.cpp@REDACTED
Tue Aug 29 19:34:33 CEST 2017


Hello,

Having a map where the value of each element it's a list :

#{ 1 => [], 2 => [], ... n => []}

and you need to append elements in the list for a specific key, what's the
most optimal way to do this without copying the lists and the map inside
the VM lot of times ?

Anything better than the following solution:

append_element(Key, Value, Map) ->
    case maps:find(Key, Map) of
        {ok, V} ->
            maps:put(Key, [Value | V], Map);
        _ ->
            maps:put(Key, [Value], Map)
    end.

Kind regards,
Silviu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170829/93591115/attachment.htm>


More information about the erlang-questions mailing list