# How to use an expression in a anonymous function?

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

Found the answer myself. The following works:

``````julia> expr=Meta.parse("x -> x^2 + 2x - 1")
:(x->begin
#= none:1 =#
(x ^ 2 + 2x) - 1
end)
julia> map(eval(expr), [1,3,-1])
3-element Array{Int64,1}:
2
14
-2
``````

Sorry for the noise!