# Generate and evaluate functions programmatically

**URL:** <https://discourse.julialang.org/t/generate-and-evaluate-functions-programmatically/110083>\
**Category:** Performance\
**Tags:** metaprogramming\
**Created:** [February 11, 2024, 11:39pm UTC](https://discourse.julialang.org/t/generate-and-evaluate-functions-programmatically/110083 "2024-02-11T23:39:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![VPBML](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vpbml/32/212617_2.png) [@VPBML](https://discourse.julialang.org/u/VPBML)\
**Post date:** [February 11, 2024, 11:39pm UTC](https://discourse.julialang.org/t/generate-and-evaluate-functions-programmatically/110083/1 "2024-02-11T23:39:24Z")

</div>

Hi,  
I wonder what is the best way (performance-wise) to generate and evaluate functions programmatically. The functions I need are generated dynamically via a Genetic Programming algorithm (i.e., they are based on previous other functions that are quite “good”, but with little changes here and there).  
I came up with the piece of code below and I doubt that the performance is far from optimal, but I am not sure how to improve it. For example, the current code is in global scope. How to make it non-local? When I tried to put it in a function, the world age error shows up. **Base.invokelatest(f, args…)** can fix that, but any better way?

```julia
f = :(function g(x,y)
       z = 1
       return z
       end)

# prev_expr is a vector of previous "good" Expr, such as :(2x + y) #
# mutation is a function to create mutation in an Expr #
now_expr = [mutation(prev_expr[i]) for i in 1:100]

# Substitute its Expr in now_expr to previously defined function f #
# and call function eval_expr( ) to evaluate its performance. #
# eval_expr() is a custom made function. #
for i in 1:100
     # substitute z = 1 by z = :(...) with (...) from now_expr #
     f.args[2].args[3].args[2] = now_expr(i)  
     # save performance in a vector arr_perf for later use # 
     arr_perf[i] = eval_expr(eval(f))                
end
...

```

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [February 12, 2024, 10:43am UTC](https://discourse.julialang.org/t/generate-and-evaluate-functions-programmatically/110083/2 "2024-02-12T10:43:29Z")

</div>

If you are ok with limiting the “primitives” used to construct your functions, I think the approach from DynamicExpressions.jl is good for performance (ie don’t use quoted code, use runtime values). SymbolicRegression.jl optimizes them (also with a genetic algorithm iirc).

---

<div class="post-metadata">

**Author:** ![VPBML](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vpbml/32/212617_2.png) [@VPBML](https://discourse.julialang.org/u/VPBML)\
**Post date:** [February 12, 2024, 12:27pm UTC](https://discourse.julialang.org/t/generate-and-evaluate-functions-programmatically/110083/3 "2024-02-12T12:27:55Z")

</div>

DynamicExpressions.jl seems to be what I am looking for. Thank you!
