On Thursday, 24 December 2020 at 14:31:22 UTC Jan Mercl wrote:

> On Thu, Dec 24, 2020 at 3:12 PM Arnaud Delobelle <arn...@gmail.com> 
> wrote: 
> > That's interesting, thanks. Although I don't think it fits my use case 
> because I need something stronger than an iterator: a function that given a 
> table and a key returns the next key in the table (as this is a public API 
> that Lua provides, see https://www.lua.org/manual/5.3/manual.html#pdf-next). 
> From my point of view this is an unfortunate API! Nevertheless it is what 
> it is... I haven't found even a hacky way to obtain that for a Go map. That 
> is why I think I need to make my own hashtable implementation 
>
> The linked pdf seems to indicate that Lua's 'next' can be implemented 
> using Go range over a map with no problem because the iteration order 
> is undefined in Lua as well. 
>
> What am I missing? 
>

The order is undefined but stable (provided you don't insert new values), 
and accessible via the `next` function.  Here is an example:

$ lua
Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
> t = { x = 1, y = 2, z = 3 }
> next(t, 'y')
z       3
> next(t, 'z')
x       1
> next(t, 'y')
z       3
> next(t, 'z')
x       1

I don't understand how to do that with a map iterator unless get a new 
iterator each time and consume it till I get the key I want to find the 
next key for (which would be very inefficient), or there is a way to 
initialise the iterator at a given key - but I haven't found how to do the 
latter.

Arnaud

 

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/31930063-9c33-4e0f-8372-ee5ae991b3e1n%40googlegroups.com.

Reply via email to