How do you make an arbitrarily nested for loop?

Use a tuple of ranges with CartesianIndices:

rngs = (10:-1:0, 10:-4:2, 10:-3:1)
for i in CartesianIndices(rngs)
    inds = Tuple(i)
    @show inds
end

(You ideally want a tuple and not an array for this sort of thing, because you want the length — i.e., the number of nested loops — to be known to the compiler.)

4 Likes