ry-dgel
September 27, 2019, 3:07am
2
I’ve already read through the following threads, and while giving solid advice, I feel that they don’t entirely apply to my situation, and have yet to tease out a working solution from the information they provide…
reflection, julia
Suppose I have
expr = :(sin(x)+cos(x))
How can I get a function looking like
x->sin(x)+cos(x)
I have tried to write such a macro
macro expr2fn(expr, arg)
return :($arg -> $(expr))
end
but it does not work
julia> f = @expr2fn expr x
(::#63) (generic function with 1 method)
julia> f(1.0)
:(sin(x) + cos(x))
Not sure what is happening here.
I have a situation where at some point during runtime a string is parsed and eval’d to an anonymous function, e.g.
fa = eval(parse("x -> mean(x)"))
Using Julia v0.5, I could then call fa([1,2,3]) later in the same routine without a problem. In v0.6 however, such a call results in a MethodError mentioning the “world age”. Reading the docs suggests using:
Base.invokelatest(fa, [1,2,3])
which works, but, according to the docstrings for invokelatest the type of the output from from the call to f…