Professional Documents
Culture Documents
Programming in Lua - 7.4
Programming in Lua - 7.4
Programming in Lua
Part I. The Language
Chapter 7. Iterators and the Generic for
function allwords ()
end
state.pos = e + 1
return string.sub(state.line, s, e)
end
end
end
Whenever it is possible,
you should try to write stateless iterators,
those that
keep all their state in the for variables.
With them, you do not create new
objects when you start a loop.
If you cannot fit your iteration into that model,
then you should try closures.
Besides being more elegant,
typically a closure is
more efficient
than an iterator using tables:
First, it is cheaper to create a
closure than a table;
second, access to upvalues is faster than
access to table
fields.
Later we will see yet another way to write iterators,
with coroutines.
This is the most powerful solution,
but a little more expensive.