[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: ipairs and extending numeric for loop
- From: Klaus Ripke <paul-lua@...>
- Date: Sat, 22 May 2010 14:49:28 +0200
now that everybody hates to write
for i = 1, #t do
v = t[i] -- optional: if nil == v then break end
while others argue that deprecating ipairs is good
since numeric for does the same and faster,
how about having numeric for actually do the same?
Since the argument "you don't need that, just use
a generic for with ipairs" is passing away,
there seems to be room for variants using multiple left hand values
for v, i = exp [, start [, limit [, step]]]
running through limit, if given, or until v becomes nil else.
for v1, ... vn, i = e1, ... en [, start [, limit [, step]]]
That would give
for v, i = t[i], 1, #t -- as above, runs to #t
for v, i = t[i], 1 -- breaks on v == nil
for v, i = t[i] -- start defaults to 1
the last looking a bit like the array part of a 4.0 table loop.
Note this deviates from other for loops in that exp is not evaluated once,
but on every turn, as in while and repeat.
Using an expression for step would almost make it a C for.
Yet it would be fast and concise, no?