lua-users home
lua-l archive

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

On Tue, 2010-11-23 at 15:50 +0100, Axel Kittenberger wrote:
> Optically I like pipes too, since they also remind me of their use in
> the shell to "tie things together". Their main problem tough is they
> cannot nest, as the begin and end token is identical. One could say,
> if you want nesting use full function syntax please, but well maybe
> someone has a problem with it not working.
> | a, b => | x => x* x * x | (a+b) |
> -> supposedly a real challenge for the parser.

Metalua solves this really nicely, it also uses pipes [1], but in a
different way - it uses it to enclose the parameter list:

"|a,b| a + b" means "function (a,b) return a + b end"

The return is implicit, so "|args| exp" is actually "function(args)
return exp end". It does not allow to use statements (you have to write
the full "function() ... end" for that), but does nest nicely:

|x||y| x + y

translates to:

  return function(y)
    return x + y

One disadvantage with this syntax is that it is not clear what happens
with multiple return values, i.e.:

print(|| true, false)

Is this 1 argument to print() - an anonymous function with 2 return
values, or 2 arguments - one function with 1 return value and the other

Currently Metalua uses the second option - it allows only 1 expression,
because the other option caused problems [2].