lua-users home
lua-l archive

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


2013/1/8 steve donovan <steve.j.donovan@gmail.com>:

> Anonymous functions in Lua are very cool, pity about the need to use
> three keywords.  If it's for an in-house project, maybe use the short
> lambda patch?

Where is the Lua 5.2 version available?

That is, |x| x^2 is fully equivalent to function(x)
> return x^2 end.
>
> Alternatively, macro preprocessing. E.g. in Luamacro you can implement
> wait(GetPlayerHealth(index)<50) easily, and the short lambda syntax is
> \x(x^2)
>
> (Yes, I would like this for Christmas, any Christmas, but people get
> surprisingly excited about this)

We don't get excited, we just say why the heck?

The syntax `lambda"x,y->x^2,x*y,y^2"` is available without any
need  for patches, with the name `lambda` and the arrow string
`->` customizable, by using the following code, hereby distributed
under the WTFYL public license:

lambda = function(def)
   local arrow = "->"
   local args,result = def:match("(.*)"..arrow.."(.*)")
   return load(args.."=... return "..result)
end

Unless `lambda` is used inside a loop that will be executed
millions of times, the execution penalty compared to
a patch is negligible.