[erlang-questions] lists:mapfind?

Lukas Larsson lukas@REDACTED
Mon Aug 21 18:18:56 CEST 2017

I agree with Richard that lists:first/2[1] is missing. Adding that
functions could solve part of the problem. maps:has/3 could also be useful,
though so far I've always found the same functionality to be easier to do
with syntax.

For me, adding lists:mapfind/3 and friends to the stdlib is a convenience
so that I don't end up writing the following in my projects:

%% Map helpers
mapsort(Key, Maps) ->
    lists:sort(fun(#{ Key := V1 }, #{ Key := V2 }) ->
                       V1 < V2
               end, Maps).

mapfind(Key, Value, List) ->
    lists:find(fun(#{ Key := V } ) -> V == Value end, List).

If it is only me and Max that find that we keep re-implementing these
functions in various places, then I don't think that we should add special
functions for list of maps. However as maps are gradually replacing more
and more use-cases where before we used proplists or records, I think these
functions will be become increasingly useful. I could very well be wrong

As for the naming of the functions, I don't have any great suggestions
about what they should be called or where they should be placed. I don't
find the lists module to be a very wrong place to put them (for the reason
Roger pointed out), the maps module also makes some sense, or possibly
adding a new module all together.

[1]: though I think it should be called lists:find/2 for consistency with
lists:keyfind/3 and to not be confused as the opposite of lists:last/1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170821/d5be1c98/attachment.htm>

More information about the erlang-questions mailing list