I would like to create a function (call it fcreate) that when given a string, returns a Lua function. For example, I should be able to say
f=fcreate("math.sin(x)+math.cos(x)")
print(f(2)) -- evaluates sin(2)+cos(2)
print(f(3)) -- evaluates sin(3)+cos(3)
To make things easy, the string will just be a function of x.
I have tried the following but it did not work:
function fcreate(fs)
assert(loadstring("local f=function (x) return ".." end"))
return f
end
For some reason the f which is returned is nil.
You almost had it right. Try this