lua-users home
lua-l archive

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

Am 10.02.2015 um 15:15 schröbte Jonathan Goble:

Quote from Philipp Janda, Fri, 06 Feb 2015 08:47:05 +0100:
"I'd rather have it documented whether an iteration can be restarted at a
certain point given only the iterator function, the state, and the current
iteration variable. Because that's the other nice feature of the current
iterator protocol. This is possible for e.g. `ipairs`, but not
`string.gmatch` or `io.lines`."

Notice how the second round resumes where the first left off, requiring
just the iterator function (here, "iter"). Is this what you're looking for,
or am I missing something?

That's how all iterators work (some need the extra two values of the iterator triplet though). No, I meant something like this:

    > t = { 1, 2, 3 }
    > f, s, var = ipairs( t )
    > for i, v in f, s, var do
    >> if i == 2 then break else var = i end
    >> end
    > for i, v in f, s, var do print( i, v ) end
    2	2
    3	3
    > for i, v in f, s, var do print( i, v ) end
    2	2
    3	3

I.e. you can re-run (part of) the iteration at a later time.