I think theoretically, you might be able to optimize it away (at least in the overwhelming majority of use-cases) by storing a couple of `*reflect.MapIter` for the last used tables in the context of the VM and then check, when `next` is called, if the key passed is the one it's currently at. But I agree, that it's pretty hackish and also doesn't give you the "stable, unless the elements are changed" property.
So, I tend to agree that you won't be able to do without your own hashtable. Which is a shame, as the builtin map is pretty good. On Thu, Dec 24, 2020 at 4:08 PM Arnaud Delobelle <arno...@gmail.com> wrote: > > > 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 > <https://groups.google.com/d/msgid/golang-nuts/31930063-9c33-4e0f-8372-ee5ae991b3e1n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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/CAEkBMfEoMYKTTrfxvbPUpypgCXAibHuWZqQeruPzw3Kr6MsiMw%40mail.gmail.com.