lua-users home
lua-l archive

Re: Passing expression as a parameter

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


On Wednesday, January 09, 2013 12:18:16 PM Dirk Laurie wrote:
> Unless `lambda` is used inside a loop that will be executed
> millions of times, the execution penalty compared to
> a patch is negligible.
Which is why you memoize the string.
local lambdacache = {}
lambda = function(def)
 local closure,error = lambdacache[def]
 if closure then return closure end
 local arrow = "%->"
 local args,result = def:match("^%s*(.*)"..arrow.."(.*)$")
 if not args then return nil,"expected -> in lambda expression" end
 if args~="" then args = args.."=... " end
 closure,error = load(args.."return "..result)
 if closure then lambdacache[def] = closure end
 return closure,error
end
Modified to allow lambda"->foo". It should also recursively expand the 
expression so you could do lambda"x->y->foo(x,y)"
-- 
tom <telliamed@whoopdedo.org>

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