Thanks Stefan, I knew there was a cleaner way. Looks like I need to study Exprs further.
As a side-note, Janis, you may want to check out the ForwardDiff <http://www.juliadiff.org/ForwardDiff.jl/perf_diff.html> package, which allows for cheap, precise derivative evaluation. On Thursday, October 22, 2015 at 12:44:15 PM UTC-5, Stefan Karpinski wrote: > > Doing it with strings and parsing is not necessary (and will make all good > Lispers very sad). You should splice and expression object into a function > definition and eval instead: > > julia> ex = :(2x + y) > :(2x + y) > > julia> @eval f(x,y) = $ex > f (generic function with 1 method) > > julia> f(3,4) > 10 > > > On Thu, Oct 22, 2015 at 1:38 PM, Alex Ames <alexande...@gmail.com > <javascript:>> wrote: > >> There may be a slicker way to do this, but this should work: >> >> julia> fngen(expr,fn) = eval(parse(string(fn)* "=" * string(expr))) >> fngen (generic function with 1 method) >> >> julia> expr = :(x + y) >> :(x + y) >> >> julia> fngen(expr,:(f(x,y))) >> func (generic function with 1 method) >> >> julia> f(2,2) >> 4 >> >> On Thursday, October 22, 2015 at 11:10:09 AM UTC-5, Jānis Erdmanis wrote: >>> >>> I am implementing boundary element method with curved elements. As it is >>> daunting task to evaluate derivatives I thought about using `Calculus` >>> symbolic differentiation which as output gives expression. Now I need to >>> convert this expression to a function, but how can I do it? >>> >>> As an example consider >>> expr = :(x + y) >>> how can I convert it to the function? >>> function f(x,y) >>> # Some magic here >>> end >>> >>> >>> >