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

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!

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

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

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)

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!