[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Passing expression as a parameter
- From: Dirk Laurie <dirk.laurie@...>
- Date: Wed, 9 Jan 2013 19:18:16 +0200
2013/1/8 steve donovan <firstname.lastname@example.org>:
> 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
> (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)
Unless `lambda` is used inside a loop that will be executed
millions of times, the execution penalty compared to
a patch is negligible.