lua-users home
lua-l archive

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


Am 17.08.2014 um 17:57 schrieb Dirk Laurie:

I thought of 'pairs' as something provided for the convenience
of idiots like me who do not understand the generic 'for' and
magically provide what is needed. `ipairs`, to me, was similar,
but not really necessary because one could also use the
arithmetic for (I started just as 5.2 reached alpha, so #tbl
was available).

Later, when I started mastering the generic 'for', `pairs` and
`ipairs` became examples of parametrized iterators. I now
think that I understand the generic `for`, so I no longer use
`pairs` and and only the absence of the more basic
ties me to `ipairs`.

But why? The public interface is clearly the iterator constructor (like `ipairs`) not the iterator triplet -- even if the iterator function in the specific case of `ipairs` and `pairs` is side-effect free, and the state is constant and trivial to create. You still use `string.gmatch` and/or `io.lines`, or do you somehow create the iterator closures by hand? And what about all the people who read your code and have not yet mastered the generic 'for' ...?

Actually I would go in the opposite direction: Having `rawpairs` (and maybe `isempty`), I wouldn't miss `next` at all. It has such a strange interface anyway, and AFAICS it's the only reason why the generic 'for' loop protocol can't evolve. E.g. a common idiom in lua-fun[1] is to ignore the first loop variable, because it is often a private value used to make the iteration restartable ("pure functional").

Philipp

  [1]: https://github.com/rtsisyk/luafun