lua-users home
lua-l archive

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


2014-08-15 22:35 GMT+02:00 Jay Carlson <nop@nop.com>:

> I don't know if this makes things simpler or more complicated.
>
> The behavior of pairs() on tables is defined in terms of next().
>
> Imagine the behavior of ipairs() were defined by inext().

I would like that. Default `inext` simply from 1 onwards until
absent, any respect paid to metamethods sits in __inext.
Actually, if we have such an `inext`, I won't care what
happens to `ipairs`.

> What do useful non-array iterators, say, non_ipairs() and
> non_inext() look like? I see two alternatives:
>
> 1) non_inext() is defined as next() but skipping integral keys.
> 2) non_inext() is defined as returning all elements which would
> not be returned by inext().

One way of making iteration for non-integers (in fact, for all
keys that fail a certain specified test) easy is to define
__newindex as a function storing items that satisfy the test
in a table __index, otherwise rawsetting them, __ipairs
to return ipairs(__index). Lua 5.2 only, of course, if the
5.3.0 alpha modification to ipairs goes through.

Then pairs(tbl) iterates the non-integers and ipairs(tbl) the
integers.