[erlang-questions] lists module functions for maps?

Karolis Petrauskas k.petrauskas@REDACTED
Mon Sep 23 14:02:13 CEST 2019


You can use a match spec to filter the list (
http://erlang.org/doc/man/ets.html#match_spec_run-2).
Pattern matching works well with lists of maps.
That's in the case, if you need to keep your data in a list instead of map
(eg. by username).

Karolis

On Mon, Sep 23, 2019 at 2:55 PM Andreas Schultz <
andreas.schultz@REDACTED> wrote:

> Am Mo., 23. Sept. 2019 um 13:15 Uhr schrieb Brujo Benavides <
> elbrujohalcon@REDACTED>:
>
>> There is some *historical* context at play here, Karl.
>> Records (which are, in many occasions, the predecessors of maps) are
>> tuples and therefore it’s not uncommon to find code that uses the functions
>> listed by Vance to work with them. For instance…
>>
>> find_user(UserName, Users) ->
>> lists:keyfind(UserName, #user.username, Users).
>>
>> If we move from records to maps as our means to implement users, then we
>> need something like…
>>
>> find_user(UserName, Users) ->
>> case lists:dropwhile(fun(#{username := UN}) -> UN =/= UserName end,
>> Users) of
>> [] -> false;
>> [User|_] -> User
>> end.
>>
>
>
>> It would be *nice* if we don’t need to do that and instead can do…
>>
>> find_user(UserName, Users) ->
>> lists:keyfind_for_maps(UserName, username, Users).
>>
>
> Your example doesn't make sense to me. Why don't you simply use a map with
> the username as key?
>
> Maps are a Key-Value stores. If you need secondary indices, you can always
> maintain additional maps for them. Non-unique secondary keys can be
> implemented by using a maps as value. Or use an ets where you can match on
> additional fields.
> Adding a linear search like lists:keyfind to maps make no sense in
> my opinion.
>
> Talking about ets, adding a match like API to maps might make sense. If
> only to make it possible to replace ets with maps without having to change
> to much code.
>
> Regards
> Andreas
>
>>
> [...]
>
> --
>
> Andreas Schultz
>
>
> _______________________________________________
> 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/20190923/53210f18/attachment.htm>


More information about the erlang-questions mailing list