lua-users home
lua-l archive

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


2017-03-01 1:18 GMT+02:00 Tim Hill <drtimhill@gmail.com>:

> Very nice idea, and very clean, though I worry about the
> performance.

If performance is an issue, simply code the two loops every time.

      for k in next,tbl do
        for m in next,tbl,k do
          -- use k and m
        end
      end

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