# How to create a function from a vector of expressions?

**URL:** <https://discourse.julialang.org/t/how-to-create-a-function-from-a-vector-of-expressions/73457>\
**Category:** New to Julia\
**Tags:** macros, metaprogramming\
**Created:** [December 21, 2021, 9:38pm UTC](https://discourse.julialang.org/t/how-to-create-a-function-from-a-vector-of-expressions/73457 "2021-12-21T21:38:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [December 21, 2021, 9:38pm UTC](https://discourse.julialang.org/t/how-to-create-a-function-from-a-vector-of-expressions/73457/1 "2021-12-21T21:38:50Z")

</div>

From  
[How do I create a function from an expression - General Usage - JuliaLang](https://discourse.julialang.org/t/how-do-i-create-a-function-from-an-expression/12349)

```julia
function genfun(expr, args::Tuple, gs=gensym())
    eval(Expr(:function,Expr(:call, gs, args...),expr))
    (args...) -> Base.invokelatest(eval(gs), args...)
end

e3 = :(a + b + c)
add3 = genfun(e3, (:a,:b,:c))
add3(1,2,3) # -> 6

```

I would like to extend to a vector of expressions, evaluating one expression after the other, like (without function generation):

```julia
evec = [:(a = 1 + 2), :(b = 2*a)]
foreach(eval, evec)
b # -> 6

```

How to create a corresponding function like above?

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [December 21, 2021, 10:46pm UTC](https://discourse.julialang.org/t/how-to-create-a-function-from-a-vector-of-expressions/73457/2 "2021-12-21T22:46:58Z")

</div>

If you wanted to compose all of the expressions into one super-expression, `:block` is the construct. `dump` is a useful tool for learning these sorts of things.

```julia
julia> quote; a = 1 + 2; b = 2*a; end |> dump
...
# dump shows that a multiline Expr has :block as the top-level construct

julia> Expr(:block,evec...)
quote
    a = 1 + 2
    b = 2a
end
# the quote here matches the above - or you can dump to see in more-precise detail

```

So this assembles the `Vector{Expr}` into a single `Expr` object. It appears your `genfun` can take you from there.

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [December 22, 2021, 5:46am UTC](https://discourse.julialang.org/t/how-to-create-a-function-from-a-vector-of-expressions/73457/3 "2021-12-22T05:46:39Z")

</div>

[dump again](https://discourse.julialang.org/t/how-to-rewrite-this-macro/73333/2) (pun intended)  
Thanks a lot!

```julia
evec = [:(a[3] = a[1] + a[2]), :(a[4] = a[2] + a[3])]
eblk = Expr(:block,evec...)
fib = genfun(eblk, (:a,))
a = [1,1,0,0]
fib(a)
a

4-element Vector{Int64}:
 1
 1
 2
 3

```

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [December 22, 2021, 5:16pm UTC](https://discourse.julialang.org/t/how-to-create-a-function-from-a-vector-of-expressions/73457/4 "2021-12-22T17:16:35Z")

</div>

Continued in  
[https://discourse.julialang.org/t/executing-a-vector-of-expressions-some-benchmarks/](https://discourse.julialang.org/t/executing-a-vector-of-expressions-some-benchmarks/)
