lua-users home
lua-l archive

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


Iterators are not bound to for loops.

for k, v in ipairs( t )
do
    print(k, v )
end

is syntactic sugar for:

local it = ipairs( t )
local k, v = it( t, 0 )
while k ~= nil
do
    print(k, v )
    k, v = it( t, k )
end

is syntactic sugar for:

local it = ipairs( t )
local k, v
k, v = it( t, 0 )
::loop::
print(k, v )
k, v = it( t, k )
if( k ~= nil )
then
    goto loop
end



On Tue, Jun 10, 2014 at 11:08 PM, Jay Carlson <nop@nop.com> wrote:

On Jun 10, 2014 2:41 PM, "Axel Kittenberger" <axkibe@gmail.com> wrote:

> for loops always have been syntatic sugar easily replaceable with while loops (or now you can replace both even with goto loops).

Proper tail recursion means control flow may be expressed in function application via CPS-transform, and rather directly replacing while-loops. And foreach() was the loop construct once...

The for-loop gives terse syntax for binding locals using the iterator protocol. It's pretty convenient. The iterator protocol is one of the few points of commonality likely to be shared between code written by multiple authors independently.

An ongoing theme: how few details must independently-written code share in order to work together syntactically?