lua-users home
lua-l archive

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


> Counter proposal:
>     f1 !  expression2 ! expression1
> or
>    f1 ! (expression-list2) ! (expression-list1)
>
> creates a anonymous function and you can call it with one or more parameters, and where express2 can be a function or simple value, or a list of expressions...

It's too complex. It just special symbol to identify next expression
is function like quotes or braces. And if previous value is function
it could be used as argument for it.
!function(...) body() end is absolutely same to function(...) body() end
except it could be passed to function as argument without brackets.

fn(1,2,3) --> fn(1,2,3)
fn"text" --> fn("text")
fn{x=1,y=2} --> fn({x=1,y=2})
fn!function() end --> fn(function() end)

It could be chained if function return function.

Symbol "@" just syntax sugar for brevity.
@ ... end --> !function() ... end
@(arg) ... end --> !function(arg) ... end

fn@ body() end -> fn(function() body() end)
fn@(args) body() end -> fn(function(args) body() end)

Them could be used to define block operations:

tree:for_all_node@(node) show(node) end
scope@(auto) f=auto(io.close){ io.open "file" } end
windows:setcallback@ body() end
try@ body() end
try@(except,finally)
  body()
  except@(err) onerror() end
  finally@ print "done" end
end
t1=task@ body() end
t1:resume()

function do_twice(fn) fn() fn() end
do_twice@ print "hello" end

function my_repeat(n) return function(fn) for i=1,n do fn(i) end end
my_repeat(10)@ print "+++" end

ignorable=pcall
ignorable@ body() end

function profile:measure(prm)
  if type(prm)=='table' then return function(fn) return
self:estimate(fn,prm) end
  if type(prm)=='function' then return self:estimate(fn) end
  error "invalid argument"
end
profile:measure@ body() end
profile:measure{ args }@ body() end

and so on.