<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">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.:</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">maps_apply(Key, Fun, Default, Map) -></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"> case maps:find(Key, Map) of</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"> {ok, Value} -></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"> maps:put(Key, Fun(Value), Map);</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"> _ -></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"> maps:put(Key, Default, Map)</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"> end.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Also possibly have functions similar to the ones in ets dealing with increments of counters whose values are maintained in the map:</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">increment(Key, Inc, Default, Map) -></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"> case maps:find(Key, Map) of</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"> {ok, Value} -></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"> maps:put(Key, Value+Inc, Map);</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"> error -></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"> maps:put(Key, Default, Map)</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"> end.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">increment(Key, Pos, Inc, InitValueFun, Map) when is_function(InitValueFun, 1) -></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><div class="gmail_default"> case maps:find(Key, Map) of</div><div class="gmail_default"> {ok, Value} when is_tuple(Value), tuple_size(Value) >= Pos -></div><div class="gmail_default"> ok;</div><div class="gmail_default"> error -><br></div><div class="gmail_default"> Value = InitValueFun(Key)</div><div class="gmail_default"> end,<br></div><div><div class="gmail_default"> V = setelement(Pos, Value, element(Pos, Value)+Inc),</div><div class="gmail_default"> maps:put(Key, V, Map).</div></div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Apr 13, 2016 at 9:40 AM, Björn-Egil Dahlberg XB <span dir="ltr"><<a href="mailto:bjorn-egil.xb.dahlberg@ericsson.com" target="_blank">bjorn-egil.xb.dahlberg@ericsson.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div text="#000000" bgcolor="#FFFFFF">
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.<br>
<br>
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).<br>
<br>
I think we should add it to the API without thinking about the
performance too much. Performance can always be improved later on.<br>
<br>
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.<br>
<br>
// Björn-Egil<br>
<br>
<div>On 2016-04-13 13:09, Jesper Louis
Andersen wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">
<div class="gmail_extra"><br>
<div class="gmail_quote">On Fri, Apr 8, 2016 at 8:31 PM,
Björn-Egil Dahlberg <span dir="ltr"><<a href="mailto:wallentin.dahlberg@gmail.com" target="_blank"></a><a href="mailto:wallentin.dahlberg@gmail.com" target="_blank">wallentin.dahlberg@gmail.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">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?"</blockquote>
</div>
<br>
</div>
<div class="gmail_extra">One function I like to have is "apply
update via function", and I implement it somewhat often:<br>
<br>
</div>
<div class="gmail_extra">maps_apply(F, K, Map) -><br>
</div>
<div class="gmail_extra"> V = maps:get(K, Map),<br>
</div>
<div class="gmail_extra"> maps:put(K, F(V), Map).<br>
<br>
</div>
<div class="gmail_extra">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.<br>
</div>
<div class="gmail_extra"><br clear="all"><span class="HOEnZb"><font color="#888888">
<br>
-- <br>
<div>J.</div>
</font></span></div><span class="HOEnZb"><font color="#888888">
</font></span></div><span class="HOEnZb"><font color="#888888">
<br>
<fieldset></fieldset>
<br>
<pre>_______________________________________________
erlang-questions mailing list
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a>
</pre>
</font></span></blockquote>
<br>
</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>