For-loop over two arrays, sequentially

I want to write code like this:

for i in vcat(vector1, vector2)
    do_something(i)
end

But I want to avoid the allocation of a new array in vcat. What’s a good way to do this?

for v in (v1, v2)
    for el in v
        do_something(el)
    end
end

Or use Iterators.flatten: Iteration utilities · The Julia Language

for el in Iterators.flatten((v1, v2))
    do_something(el)
end
3 Likes

FWIW, from this related thread, vcat allocates but seems to be faster than Iterators.flatten.

2 Likes