[erlang-questions] maps:first

Robert Virding rvirding@REDACTED
Thu Sep 26 00:14:15 CEST 2019


I have a specific need for it. I need 2 functions working on maps which
allow me to step over all the key/value pairs in a map without making an
iterator.

What I need is a maps:first(Map) and then a maps:next(Key, Map) which takes
a map and a key and gives me the next key/value pair WITHOUT an iterator.
All I have is the map and a key.and cannot keep an iterator. Yes, it could
be done by generating an iterator each time and stepping down it but not
efficiently. The order would be irrelevant just so long as I can be just to
get all the keys as long as the map is not updated while stepping over it.
If the map is updated then all bets are off.

This is the only thing which stops me from using maps inside Luel to
represent Lua tables and it is most annoying.

Robert


On Wed, 14 Aug 2019 at 09:52, Andreas Schultz <
andreas.schultz@REDACTED> wrote:

> Am Di., 13. Aug. 2019 um 20:18 Uhr schrieb Oliver Bollmann <
> oliver.bollmann@REDACTED>:
>
>> 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
> random.
>
> 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
> performance.
>
> Regards
> Andreas
>
> 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
>
> Germany
>
> 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
>
>
> _______________________________________________
> 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/20190926/83b9daa6/attachment.htm>


More information about the erlang-questions mailing list