[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Abusing Lua iterators for fun and profit
- From: Jonathan Goble <jcgoble3@...>
- Date: Tue, 10 Feb 2015 09:26:48 -0500
(Note that I only just subscribed to the list this morning, so I can't
properly quote or thread this because I don't have the original email
to click reply on. This quote is therefore a copy and paste from
<http://lua-users.org/lists/lua-l/2015-02/msg00089.html>.
Also, this is a re-send because according to
<http://listmaster.pepperfish.net/cgi-bin/mailman/private/lua-l-lists.lua.org/2015-February/046605.html>
the first message apparently got encoded in base64. This time I have
the proper "plain text mode" option selected in Gmail, so crossing my
fingers that it works this time.)
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`."
I haven't tested io.lines, but this interactive session in Lua 5.1
works with string.gmatch:
> str = 't1t2t3t4t5'
> iter = str:gmatch('t%d')
> for match in iter do
>> print(match)
>> if match == 't3' then
>> break
>> end
>> end
t1
t2
t3
> print 'Doing other stuff...'
Doing other stuff...
> for match in iter do
>> print(match)
>> end
t4
t5
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?