[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Iterating pairs of keys from a table
- From: Dirk Laurie <dirk.laurie@...>
- Date: Wed, 1 Mar 2017 07:43:24 +0200
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