Here's a rewrite based on the manual's definition of the generic for (a
better approach):
A for statement like
for var_1, ···, var_n in explist do block end
is equivalent to the code:
do
local f, s, var = explist
--proposed addition
if type(f)~= "function" then
local meta_f = metatable(f).__iter
if not meta_f and type(f)=="table" then meta_f = next end
if meta_f then f, s, var = meta_f, f, s end
end
while true do
local var_1, ···, var_n = f(s, var)
var = var_1
if var == nil then break end
block
end
end