The following works:
julia> map(x -> x^2 + 2x - 1, [1,3,-1])
3-element Array{Int64,1}:
2
14
-2
But I need to calculate the function dynamically.
I have:
julia> expr=Meta.parse("x^2 + 2x - 1")
:((x ^ 2 + 2x) - 1)
Now I try to use this expression in a map operation,
but it fails:
julia> map(x -> expr, [1,3,-1])
3-element Array{Expr,1}:
:((x ^ 2 + 2x) - 1)
:((x ^ 2 + 2x) - 1)
:((x ^ 2 + 2x) - 1)
I tried to use eval but it doesn’t help.
julia> map(eval(x -> expr), [1,3,-1])
3-element Array{Expr,1}:
:((x ^ 2 + 2x) - 1)
:((x ^ 2 + 2x) - 1)
:((x ^ 2 + 2x) - 1)
Any idea how to create an anonymous function from an expression that gives the same result as my first example?
Thank you!
Uwe