Iterate through all combinations of elements in an arbitrary number of arrays

I have an array arrays which contains a number of arrays (arbitrary). How do I iterate through all combinations of elelements? If I know that there are e.g. 3 arrays I can do:

for e1 in arrays[1], e2 in arrays[2], e3 in arrays[3]
    do_stuff([e1,e2,e3])
end

but if the number is arbitrary, what do I do then?

1 Like

You can use Iterators.product:

for elements in Iterators.product(arrays...)
  do_stuff(elements)
end
2 Likes

That worked perfectly, loads of thanks :slight_smile: