Timothée HENRY
Timothée HENRY

Reputation: 14604

Iterators product on array of arrays

How can I create an iterator on the product of arrays, from an array of arrays? The Array size not predetermined.

Basically the following works as I wish:

for i in Base.Iterators.product([1,2,3],[4,5])
   print(i)
end
(1, 4)(2, 4)(3, 4)(1, 5)(2, 5)(3, 5)

But I would like it to work for an array of arrays, but I am getting different result:

x = [[1,2,3],[4,5]]
for i in Base.Iterators.product(x)
   print(i)
end
([1, 2, 3],)([4, 5],)

Upvotes: 8

Views: 157

Answers (1)

David Sainez
David Sainez

Reputation: 6956

You can use the splat operator to interpolate the array of arrays into the function call:

julia> x = [[1,2,3],[4,5]];

julia> for i in Base.Iterators.product(x...)
          print(i)
       end
(1, 4)(2, 4)(3, 4)(1, 5)(2, 5)(3, 5)

Upvotes: 5

Related Questions