[erlang-questions] maps:first

Andreas Schultz andreas.schultz@REDACTED
Wed Aug 14 09:51:45 CEST 2019

Am Di., 13. Aug. 2019 um 20:18 Uhr schrieb Oliver Bollmann <

> Hi,
> i guess *we* do all the same,
> 1) processes with propery Y, i have about 1-10 million processes
> 2) gen_server to manage these processes in maps,dict,lists for example a
> pool with in,out,leave,delete
> 3) monitoring these processes
> Dilemma:
> If process P died i have to delete it from data structure using in 2)
> - maps, dict very fast;
>    lists -> List -- [Item] slow!
>    OK, i could use a sorted list and and the subtract would be faster, but
> insert is then slower!
>    And subtract need an algorithm!
> - i do not want a special process, only one random or the first one from
> data structure using in 2)
>    here is the place where i need *first_from_data_structure*
>    lists very fast: first defines it itself -> [First|Rest] = List,
>    but using maps or dict there is not first, at all!

Are you sure a maps first is really what you want? Maps leys are not
ordered (beyond MAP_SMALL_MAP_LIMIT, which is 32), but they are also not

As long as your map has less elements than MAP_SMALL_MAP_LIMIT it will
behave like a ordered list, beyond that it will use a hash for ordering.
That means that for a given set of keys the ordering will be stable (but
not sorted). So when you take the first element of a map and put it back
again (based on key), it will be the new first element again.

IMHO the only use case for you maps:first function is something that tries
to distribute new requests/elements across the elements of a map. Your
maps:first function coupled with the constant ordering of map keys would
lead to a distribution that is highly skewed towards a small number of keys
(or even just one key).

Now, a maps:nth/2 function that works like list:nth/2 is another story. You
could use

   maps:nth(rand:uniform(maps:size(Map)), Map)

to get a somewhat evenly distributed key/element from a map.
Implementing this function currently requires you to convert the map to a
list first. For large maps having a maps:nth/2 should yield better


The question is: How can i get a random or first item from maps or dict?
> Hope that helps!
> Oliver
> On 13.08.19 15:41, Lukas Larsson wrote:
> On Tue, Aug 13, 2019 at 3:34 PM Oliver Bollmann <
> oliver.bollmann@REDACTED> wrote:
>> I often need maps:first(Map), is there plan for it?
> No, there is currently no plan for it, as I cannot think of a use case
> when it would be useful, but that might just be poor imagination on my part.
> What do you use maps:first for?
>> My current implementation is:
>> first(Map) ->
>>   case maps:next(maps:iterator(Map)) of    {Key,Val,_} -> {Key,Val};
>>     none -> none
>>   end.
>> Create the iterator always a new map?
>> --
>> Grüße
>> Oliver Bollmann
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
> --
> Grüße
> Oliver Bollmann
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions


Andreas Schultz


Principal Engineer

t: +49 391 819099-224

------------------------------- enabling your networks

Travelping GmbH

Roentgenstraße 13

39108 Magdeburg


t: +49 391 819099-0

f: +49 391 819099-299

e: info@REDACTED

w: https://www.travelping.com/

Company registration: Amtsgericht Stendal  Reg. No.: HRB 10578
Geschaeftsfuehrer: Holger Winkelmann VAT ID: DE236673780
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190814/a46572bd/attachment.htm>

More information about the erlang-questions mailing list