lua-users home
lua-l archive

Re: Another example for syntactically lightweight closures

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


Er, perhaps I misunderstand your examples, but those look like precisely
my case (1):
 mydata:sort ( |a,b|(upcase (a) > upcase (b)) )
 mydata:collect ( |x| (x.shoesize > x.age/2) )
Whoops, you're right, sorry :)
Hm... could something like "|a| ..." be made shorthand for "function(a) return ... end"? (with the parentheses optional?) Trouble is, it leads to "||" for argument-less closures, and it won't extend nicely for the unwind_protect multi-line example that started this thread. Having said that, I like your "|a,b|(a + b)" and "|a|{ print (a) }". The first one would be single-valued (fine with me). And arg-less could then be "||{ print (123) }". Both of them could be used after the closing parenthesis, so to get back to the original example, we'd have:
 unwind_protect( io.close, f ) || {
 print( f:read( "*a" ) )
 }
(or perhaps ":" i.s.o. "|", no idea whether that can fit in Lua's syntax rules) I sure hope someone finds a nice syntax for Lua, I expect something like the above one-liners to be very useful. Ruby is full of examples where closures simplify the notation - to the point where they become second nature, in fact.
-jcw

AltStyle によって変換されたページ (->オリジナル) /