Hi
I have a small problem regarding the dynamic creation of functions from expressions. My situation is the following: A process dynamically generates a random expression which is then evaluated on given samples and an error is calculated with regard to expected outputs. This information is later used to update the creation process for the expressions etc.
Here is what I currently do:
function run(X, Y, root)
s = sample(root) #Gives me the random expression
f = eval(:(x -> $s))
ev = [Base.invokelatest(f, x) for x in X]
return mse(ev, Y)
end
This works, but the expressions I get are pretty quick to evaluate, so I have the feeling that invokelatest
adds quite a bit of overhead (Testing for some small functions it seems that invokelatest
is about 10 times slower than just directly calling a function)
Is this really the best I can do in Julia? I just want to create a local function dynamically. It does not depend on any global things and it only has to be available in this scope, so I am slightly confused why this is made so difficult and also slow.
Or did I just miss something and this can be done in a fundamentally different way?