[erlang-questions] maps:first

Guilherme Andrade g@REDACTED
Wed Aug 14 00:10:05 CEST 2019


On Tue, 13 Aug 2019 at 19:18, Oliver Bollmann <oliver.bollmann@REDACTED>
wrote:

> 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!
>
Have you considered gb_trees[1]? Sure, for common use cases or large
(enough) data sets they're slower than maps, but they're pretty fast
notwithstanding, especially if your key types are cheap to compare.
And since it's an ordered data type, you can then simply take the smallest
pair when implementing those sort of melded-pool-and-monitor patterns.

[1]: http://erlang.org/doc/man/gb_trees.html



> 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
>


-- 
Guilherme
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190813/69ea82b6/attachment.htm>


More information about the erlang-questions mailing list