# Creating an anonymous function taking as arguments the elements in a vector

**URL:** <https://discourse.julialang.org/t/creating-an-anonymous-function-taking-as-arguments-the-elements-in-a-vector/41373>\
**Category:** General Usage\
**Created:** [June 14, 2020, 2:18pm UTC](https://discourse.julialang.org/t/creating-an-anonymous-function-taking-as-arguments-the-elements-in-a-vector/41373 "2020-06-14T14:18:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karel\_Van\_Brantegem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karel_van_brantegem/32/15662_2.png) [@Karel\_Van\_Brantegem](https://discourse.julialang.org/u/Karel_Van_Brantegem)\
**Post date:** [June 14, 2020, 2:18pm UTC](https://discourse.julialang.org/t/creating-an-anonymous-function-taking-as-arguments-the-elements-in-a-vector/41373/1 "2020-06-14T14:18:28Z")

</div>

Hi Julians

I have a vector _vector\_symbol_ that contains a variable number of elements of type _Symbol_. I also have the variable _ex_ of type _Expr_ that is an expression of the _Symbol_ elements in _vector\_symbol_.

For example:  
vector\_symbol = [:temp, :press, :mass, :speed]  
ex = : ( temp \* temp \* mass)

I want to create an anonymous function _f\_anon_ that takes as arguments the elements in _vector\_symbol_ and that maps these arguments as coded in the expression _ex_.

I was hoping that something like this would work:  
@eval f\_anon( $i for i in vector\_symbol ) = $ex  
but I get the error  
ERROR: UndefVarError: i not defined.

I could write:  
@eval f\_anon( $(vector\_symbol[1]), $(vector\_symbol[2]), $(vector\_symbol[3]), $(vector\_symbol[4]) ) = $ex  
but this is hard coded; it will not work when _vector\_symbol_ has a length different from 4.

I was not able to find a solution in the documentation of Julia about metaprogramming and also the internet didn’t help me out.

Anybody ideas?

Many thanks!

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [June 14, 2020, 4:20pm UTC](https://discourse.julialang.org/t/creating-an-anonymous-function-taking-as-arguments-the-elements-in-a-vector/41373/2 "2020-06-14T16:20:27Z")

</div>

You can use `...` to expand a collection (in this case `vector_symbol`) to multiple function arguments:

```julia
julia> vector_symbol = [:temp, :press, :mass, :speed]
4-element Array{Symbol,1}:
 :temp
 :press
 :mass
 :speed

julia> ex = :( temp * temp * mass)
:(temp * temp * mass)

julia> @eval f($(vector_symbol...)) = $ex
f (generic function with 1 method)

julia> f(1, 2, 3, 4)
3

```

---

<div class="post-metadata">

**Author:** ![Karel\_Van\_Brantegem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karel_van_brantegem/32/15662_2.png) [@Karel\_Van\_Brantegem](https://discourse.julialang.org/u/Karel_Van_Brantegem)\
**Post date:** [June 15, 2020, 8:31am UTC](https://discourse.julialang.org/t/creating-an-anonymous-function-taking-as-arguments-the-elements-in-a-vector/41373/3 "2020-06-15T08:31:36Z")

</div>

Thank you very much @rdeits!  
I took a look again at the Julia documentation, chapter Metaprogramming.  
Apparently, the solution you posted here is called ‘Splatting interpolation’. I looked over it…  
Thank you again!
