lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> On Feb 28, 2017, at 9:43 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> If you must have an iterator factory, and wish to avoid a coroutine
> (damn! I so seldom have good reason to use one, your problem
> seemed to be a case in point), it is possible to do it all in a single
> generic `for` by making your routine stateless. Exercise for the
> reader.
> 
> keypairs = function(tbl)
>  return function(tbl,k1,k2)
>    -- no upvalues required
>   return k1,k2
>  end
> end
> 

Well, you have me beaten. For a start, I was under the impression Lua only passed two arguments to the iterator function, yet you have three? Clearly I’m missing something.

I did look at cunning ways to bury the state is the (s, v) pair maintained by the for loop as a way to avoid upvalues, but I didn’t really see any advantage over upvalues from a performance perspective.

—Tim