Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterators product on array of arrays

Tags:

julia

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],)
like image 213
Timothée HENRY Avatar asked Jan 21 '20 05:01

Timothée HENRY


1 Answers

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)
like image 195
David Varela Avatar answered Oct 05 '22 03:10

David Varela