Hi all,
I’m attempting to teach myself how to do Julia meta programming. My current exercise is to write a macro that produces a variant of zip, in that you give it a single dimensional array, and it returns an iterator that gives tuples, each of which consists of elements i through i+N of the array.
In other words, iterating through @zipn([1,2,3,4,5], 3) should return, in order, (1,2,3), (2,3,4), (3,4,5).
Here’s my macro. It doesn’t work.
macro zipn(vec, N)
expr = Expr(:call, :zip)
for i=1:N
push!(expr.args, vec.args[i:end-(N-i)])
end
return expr
end
Specifically, for some reason it returns an Iterator that looks like:
Base.Iterators.Zip{Array{Any,1},Base.Iterators.Zip2{Array{Any,1},Array{Any,1}}}(Any[1, 2, 3, 4], Base.Iterators.Zip2{Array{Any,1},Array{Any,1}}(Any[2, 3, 4, 5], Any[3, 4, 5, 6]))
The thing that confuses me is that the code works like a charm when executed one line at a time in the REPL. It just doesn’t work when it’s inside the body of the macro.
Clearly I’m missing a concept. Any help figuring out what would be appreciated.