lua-users home
lua-l archive

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


The problem with compiling on the fly is that you don't get syntax errors when the main code is compiled, and you can't use local variables in your lambda (I mean, local variables from the calling function)

-----Original Message-----
From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On Behalf Of Dirk Laurie
Sent: Wednesday, January 09, 2013 9:18 AM
To: Lua mailing list
Subject: Re: Passing expression as a parameter

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.