lua-users home
lua-l archive

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


Indeed there's a lot in common between lists by comprehension and xloops, and if both where to remain in metalua, their code would be factorized. The former is mostly the functional equivalent of the latter, which is more imperative.

(the clist extension can be found at:
http://repo.or.cz/w/metalua.git?a=blob;f=src/lib/extension/clist.mlua,
with a usage sample at:
http://repo.or.cz/w/metalua.git?a=blob;f=src/samples/clist_test.mlua).

It often appears that imperative traits blend better than functional ones in Lua, for reasons I couldn't explain. Adapting ideas from CL's 'loop' macro, I'd like to try this:

    squares_to_100 = for i=1,100 collect i
    sum_to_100     = for i=1,100 accumulate i, |x,y|x+y
    biggest        = for _,v in pairs(t) accumulate v, max

(The general principle is that the "do...end" would only be one of the possible bodies of a loop, and that with some bodies, which produce values, loops must be accepted in an _expression_'s context, not only as statements. Syntactical details yet to be decided).

This would supersede the comprehension part of clist. Improved indexes/slices (such as "list[3 ... 5]") would remain relevant.

About your concern with "while/until" semantics: first, they're inspired by the CL macro (not that it guarantees tastefulness, though). Moreover, their sense seems consistent to me: they introduce a condition that's sufficient to leave a loop.

If I understand correctly, your concern is that by adding "do" / "end" keywords at various places, you change the program's meaning; I don't think it would shock any developer: if you add some "{" / "}" randomly in C/Java/C#/C++ code, you'll also change the program's meaning, and nobody would consider that a problem; that's actually their whole purpose :)

Besides, a correct editor will make the problem obvious in your double-do example, by indenting the body twice. This is actually a fairly good test for an extension: if it's lua-ish, there are good chances that emacs' lua-mode already knows how to indent it.

-- Fabien.