[erlang-questions] Wanted additions to the maps module?
Serge Aleynikov
serge@REDACTED
Wed Apr 13 17:43:07 CEST 2016
I'd like to note that if such a function is added, there should be also a
variant that doesn't throw if the key is not present in the map. It's a
pretty common use case to "apply update" or "insert", e.g.:
maps_apply(Key, Fun, Default, Map) ->
case maps:find(Key, Map) of
{ok, Value} ->
maps:put(Key, Fun(Value), Map);
_ ->
maps:put(Key, Default, Map)
end.
Also possibly have functions similar to the ones in ets dealing with
increments of counters whose values are maintained in the map:
increment(Key, Inc, Default, Map) ->
case maps:find(Key, Map) of
{ok, Value} ->
maps:put(Key, Value+Inc, Map);
error ->
maps:put(Key, Default, Map)
end.
increment(Key, Pos, Inc, InitValueFun, Map) when is_function(InitValueFun,
1) ->
case maps:find(Key, Map) of
{ok, Value} when is_tuple(Value), tuple_size(Value) >= Pos ->
ok;
error ->
Value = InitValueFun(Key)
end,
V = setelement(Pos, Value, element(Pos, Value)+Inc),
maps:put(Key, V, Map).
On Wed, Apr 13, 2016 at 9:40 AM, Björn-Egil Dahlberg XB <
bjorn-egil.xb.dahlberg@REDACTED> wrote:
> There is currently no (good) way to get out of C-context to execute some
> erlang snippet and then get back into c-context. Traps does this but only
> in a controlled manner. I think it is doable though.
>
> Your proposal of maps_apply/3 is the maps equivalent of dict:update/3 and
> sadly that name is already occupied in maps (it's the equivalent of
> gb_trees).
>
> I think we should add it to the API without thinking about the performance
> too much. Performance can always be improved later on.
>
> Is maps:apply/3 the best name we can think of? I don't really trust you
> and naming things. =) update/3,4 would probably have been best but c'est la
> vie.
>
> // Björn-Egil
>
> On 2016-04-13 13:09, Jesper Louis Andersen wrote:
>
>
> On Fri, Apr 8, 2016 at 8:31 PM, Björn-Egil Dahlberg <
> <wallentin.dahlberg@REDACTED>wallentin.dahlberg@REDACTED> wrote:
>
>> 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?"
>
>
> One function I like to have is "apply update via function", and I
> implement it somewhat often:
>
> maps_apply(F, K, Map) ->
> V = maps:get(K, Map),
> maps:put(K, F(V), Map).
>
> But it can be made much faster in a direct implementation since I don't
> have to first pick up the value: I can apply F when I sit with the value at
> hand and thus avoid the "put" lookup path. It might, however, be nasty to
> implement because F is in Erlang-context, whereas the maps operations are
> in BIF-context.
>
>
> --
> J.
>
>
> _______________________________________________
> erlang-questions mailing listerlang-questions@REDACTED://erlang.org/mailman/listinfo/erlang-questions
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160413/a67c9718/attachment.htm>
More information about the erlang-questions
mailing list