[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Abusing Lua iterators for fun and profit
- From: Dirk Laurie <dirk.laurie@...>
- Date: Fri, 6 Feb 2015 09:01:35 +0200
2015-02-06 8:22 GMT+02:00 Dirk Laurie <dirk.laurie@gmail.com>:
Ugh, trying so hard to be precise, and then there's a typo
that totally destroys the point.
> One must distinguish between an "iterator function" and an
> "iterator factory".
>
> io.lines(...), string.gmatch(str,pat) are iterator functions
> io.lines, string.gmatch are iterator factories
>
> An iterator takes two arguments, the first of which is guaranteed
> always to be the same, and returns as many values as you like,
> only the first of which affects progress of the iteration.
>
> An iterator factory takes as many arguments as you like and
> can usefully return up to three values. The second value, if
> any, will be the invariant; the third value, if any, will be the
> initial value to the generic `for`.
>
> In my opinion, if an iterator function
I mean "iterator factory".
> returns more than one
> value, that behaviour should be explicitly documented, since
> it relies on a gotcha: something that lexically looks like one
> item, but may be more. It's clever, no doubt about that, since
> it can be thought of as providing default values that you can
> always discard by using parentheses, but it is too clever by
> half for my taste.
>
> io.lines and string.gmatch don't do that, so there is no need
> to say anything about it.