[erlang-questions] maps iterator

zxq9 <>
Wed Sep 30 01:27:43 CEST 2015


On 2015年9月30日 水曜日 01:06:22 you wrote:
> Seeing we are wishing here I would like a function which when you call it
> with a key returns the next key in the map. This so I can step through the
> map one key at a time.
> 
> M = #{a => 1, b => 2, e => 5}.
> maps:first_key(M) ==> a
> maps:next(M, a) ==> {b,2}
> maps:next(M, b) ==> {e,5}
> 
> The order is irrelevant. I think the next function might as well return the
> next key and its value seeing there is no copying and very little extra
> work and you are probably going to get the value anyway. Yes, I know I can
> get a list of all the keys but then I need to keep the list somewhere and
> if it is a big map (which is now feasible) then creating a big list seems a
> waste.
> 
> Without this I can't use maps in a place where they would really fit, in
> luerl my Lua implementation.

To iterate through the map you would have to loop over it. How does the map key generator track its place in the key list without maintaining a list, being like ETS, or being in a separate process? Assuming that what you mean isn't already maps:map/2 or maybe a maps:foreach/2?

I'm trying to imagine what the calling code would look like where it isn't like maps:map/2, maps:fold/2 or a hypothetical maps:foreach/2, and maps don't become ETS... and drawing a blank.

-Craig


More information about the erlang-questions mailing list