I wanted make a macro that creates some code for me. E.g.
I have a vector x = [9,8,7]
and I want to use a macro to generate this piece of code vcat(x[1], x[2], x[3])
and run it. And I want it to work for arbitrary length vectors.
I have made the macro as below
macro some_macro(a)
quote
astr = $(string(a))
s = mapreduce(aa -> string(astr,"[",aa,"],"), string, 1:length($(a)))
eval(parse(string("vcat(", s[1:(end-1)],")")))
end
end
x = [7,8,9]
@some_macro x
The above works. But when I try to wrap it inside a function
function some_fn(y)
@some_macro y
end
some_fn([4,5,6])
It doesn't work and gives error
UndefVarError: y not defined
and it highlights the below as the culprit
s = mapreduce(aa -> string(astr,"[",aa,"],"), string, 1:length($(a)))
Edit See julia: efficient ways to vcat n arrays
for advanced example why I want to do instead of using the splat operator