lua-users home
lua-l archive

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



Why syntaxize it? Just have a function which wraps an iterator and a (probably anonymous) function and produces a filtered iterator.

The question behind syntax extensions is always: does it improve code readability?

Generally, a syntax might improve readability if it captures a recurring pattern, that is especially important to identify to understand a piece of code. For instance, numerical for loops are a nice syntax, because they capture a very common pattern, and "for i=x,y do ... end" is easier to identify than "local i=x; while i<=y do ...; i=i+1 end". But strictly speaking, there's no technical need to "syntaxize" it either.

There is a trade-off to find, between the cost of learning a new idiom, and the payback of reading it more easily. xloop is an experiment to see whether there are more idioms worth supporting with dedicated syntax in loops. I suspect there is, because many languages offer similar features:
Moreover, it seems to me that most of the time, when there are non-trivial control flow operations in a loop which make it harder to grok, it's about nesting loops, filtering, or escaping the loop early; so a more synthetic presentation of these might prove useful. Finally, the proposed extensions blend quite nicely with Lua's syntax; syntaxes that respect the language's regularity are faster learned, and therefore are more easily "worth it".

Again, this must be put back in metalua's context, where macros are considered more expensive than in Lisp, but where there it remains much more acceptable to extend the language than in Lua.

As you noticed, it would be possible to offer semantically equivalent iterators in plain Lua, but how readable would it be to write the equivalent of, say:

    for i=1,n for j=1,i until tab[i][j]==0 do
       ...
    end
?

-- Fabien.