Julia : Cartesian product of multiple arrays

Viewed 2450

I would like to compute a product iterator using Iterators.jl. Let's say I have an array of UnitRanges tab with a priori unknown size.

I would like to compute the cartesian product of the elements of tab.

For example if tab length is 2 and tab[1] = a and tab[2] = b I want to compute product(a,b) from Iterators.jl.

I want to make a generic function that compute the cartesian product of every component in tab.

I tried something like this

prod = tab[1]
for i in tab[2:end]
   prod = product(prod,i)
end

However if tab is length 3, components a,b and c, I obtain in prod elements under the form (1,(3,2)) and not (1,3,2). With 1 element of c, 3 element of b and 2 element of a.

1 Answers
Related