[erlang-questions] lists module functions for maps?

Karl Velicka karolis.velicka@REDACTED
Mon Sep 23 13:32:54 CEST 2019


Ah, of course - that makes sense. I would still like to argue that maps are
a good substitute for only some kinds of records and I see both data
structures as more orthogonal than predecessor-successor but this isn't the
right place for it and I don't want to derail the discussion (plus, I'm
sure I'd find myself out of my depth in that discussion rather quickly!).

Thanks for pointing out the obvious that I have somehow missed.

On Mon, 23 Sep 2019 at 14:15, Brujo Benavides <elbrujohalcon@REDACTED>
wrote:

> 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).
>
> Hope this helps :)
>
> ------------------------------
> *Brujo Benavides <http://about.me/elbrujohalcon>*
>
>
>
> On 23 Sep 2019, at 08:07, Karl Velicka <karolis.velicka@REDACTED> wrote:
>
> I'm afraid I can't see the equivalence in your example - I see a list of
> tuples as a list of values, whereas a list of maps is a list of containers.
> In my mind, a more apt comparison would be a list of maps vs a list of
> lists of tuples, and functions operating on the latter would strike me as
> an odd addition to stdlib.
>
> I'd be curious to know what your usecase for these functions is.
>
> Some of them can be trivially implemented:
>
> keyfind(Key, MapList) -> hd(lists:filter(fun(Map) -> maps:is_key(Key, Map)
> end, MapList).
>
> (The example implementation there is inefficient but I hope you get the
> idea)
>
> Some others, like keysort(Key, MapList1) -> MapList2 don't quite make
> sense to me as the semantics are unclear.
>
>
> I think elaborating on the use cases would help your proposal.
>
> All the best,
> Karl
>
> On Mon, 23 Sep 2019 at 09:00, Vance Shipley <vances@REDACTED> wrote:
>
>> Should we have a corollary to lists:keyfind/3 which works on lists of
>> map()?
>>
>>      keyfind(Key, MapList) -> Map | false
>>
>> ... and the rest:
>>
>>      keydelete(Key, MapList1) -> MapList2
>>      keymap(Fun, Key, MapList1) -> MapList2
>>      keymember(Key, MapList) -> boolean()
>>      keymerge(Key, MapList1, MapList2) -> MapList3
>>      keyreplace(Key, MapList1, NewMap) -> MapList2
>>      keysort(Key, MapList1) -> MapList2
>>      keystore(Key, MapList1, NewMap) -> MapList2
>>      keytake(Key, MapList1) -> {value, Map, MapList2} | false
>>
>>
>> --
>>      -Vance
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://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/20190923/92496606/attachment.htm>


More information about the erlang-questions mailing list