maps:first

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

maps:first

obi458

I often need <a class="moz-txt-link-freetext" href="maps:first(Map)">maps:first(Map), is there plan for it?

My current implementation is: 

first(Map) ->
  case <a class="moz-txt-link-freetext" href="maps:next(maps:iterator(">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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: maps:first

Lukas Larsson-8


On Tue, Aug 13, 2019 at 3:34 PM Oliver Bollmann <[hidden email]> 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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: maps:first

obi458

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!

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 <[hidden email]> 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 <a class="moz-txt-link-freetext" href="maps:first">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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
-- 
Grüße
Oliver Bollmann

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: maps:first

dmkolesnikov
Hello,

May be my comment is not extremely relevant to this discussion but

I would wish that Erlang data structures supports zipper trait. It make easy to iterate them back and force.

Dmitry 

On 13 Aug 2019, at 21.18, Oliver Bollmann <[hidden email]> 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!

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 <[hidden email]> 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 <a class="moz-txt-link-freetext" href="maps:first">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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
-- 
Grüße
Oliver Bollmann
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: maps:first

Guilherme Andrade
In reply to this post by obi458


On Tue, 13 Aug 2019 at 19:18, Oliver Bollmann <[hidden email]> 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 <[hidden email]> 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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
-- 
Grüße
Oliver Bollmann
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


--
Guilherme

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: maps:first

Andreas Schultz-3
In reply to this post by obi458
Am Di., 13. Aug. 2019 um 20:18 Uhr schrieb Oliver Bollmann <[hidden email]>:

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 <[hidden email]> 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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
-- 
Grüße
Oliver Bollmann
_______________________________________________
erlang-questions mailing list
[hidden email]
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: [hidden email]

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

   
Company registration: Amtsgericht Stendal  Reg. No.: HRB 10578
Geschaeftsfuehrer: Holger Winkelmann VAT ID: DE236673780

 


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: maps:first

Robert Virding
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 <[hidden email]> wrote:
Am Di., 13. Aug. 2019 um 20:18 Uhr schrieb Oliver Bollmann <[hidden email]>:

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 <[hidden email]> 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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
-- 
Grüße
Oliver Bollmann
_______________________________________________
erlang-questions mailing list
[hidden email]
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: [hidden email]

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

   
Company registration: Amtsgericht Stendal  Reg. No.: HRB 10578
Geschaeftsfuehrer: Holger Winkelmann VAT ID: DE236673780

 

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions