lua-users home
lua-l archive

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



Did a 'lambda' mod that follows the syntax in MetaLua, with one extension.
Blocks, not only expressions can be used within the body.

local t=
    { add= |a,b| a+b,
      sub= |a,b| a-b,

-- Blocks can be used when wrapped in some scope-declaring construct,
      -- s.a. 'if-then-else-end', 'for..end', 'do..end'

--side_effect= || c= c+1, -- not ok (comma implies more values, not end of assignment)
      side_effect= || do c=c+1 end,   --ok
      side_effect2= || if c>0 then c=c+1 else c=c-1 end,
      side_effect3= || for i=1,99 do c=c*i end,

      z= |a| if a>0 then =a+1 else =a-1 end,
    }

This should be as keyword-scarce as can get, and actually I like it.

-asko


Asko Kauppi kirjoitti 7.2.2008 kello 9:24:


What would you think of a notation like this?

{
	yyy=	do =something end
	xxx= 	do(a,b) =a+b end
	zzz= 	do(a,b,c) if a then =b else =c end end
}

Will make it a luaSub mod for trial, but thought of asking opinions first.

do ... end	->  	function() ... end
do(...) ... end	->	function(...) ... end

= exp		->	return exp		(within the enclosed block)

Especially the '=' trick is handy, since it makes simple closures readable and does not (is this true?) overlap with existing valid Lua syntax.

-asko


David Manura kirjoitti 7.2.2008 kello 4:30:

Miles Bader writes:
Bret Victor writes:
I thing I like about Lua syntax is that punctuation is kept minimal,
and words are used (in, do, end) when that's how you would read the
code out loud.

I agree, in general -- I don't dislike punctuation heavy languages like C, but Lua has a consistent and attractive style of its own. [Still, in
the middle of an expression, I think a punctuation-oriented lambda
syntax isn't out of place, even in Lua.]

In Lua, we do write "{1,2}" rather than "table 1, 2 end". Given that functions
are fundamental data type, like tables, there is consistency in using
punctuation to denote functions, particularly when used as lambda expressions.

Approaches for syntactically lightweight closures are also discussed in [1].

[1] http://lua-users.org/wiki/ShortAnonymousFunctions