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 <alexander.m.a...@gmail.com>
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
>>
>>
>>

Reply via email to