[erlang-questions] lists module functions for maps?

Brujo Benavides elbrujohalcon@REDACTED
Mon Sep 23 13:15:46 CEST 2019


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 <mailto: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 <mailto:erlang-questions@REDACTED>
> http://erlang.org/mailman/listinfo/erlang-questions <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/15333ea3/attachment.htm>


More information about the erlang-questions mailing list