Suppose you want to define an iterator over the vertices of the n-dimensional unit hypercube.
In two dimensions, this is:
julia> using Iterators
julia> collect(Iterators.product([-1, 1], [-1, 1]))
4-element Array{Tuple{Int64,Int64},1}:
(-1,-1)
(1,-1)
(-1,1)
(1,1)
and similarly for 3d:
julia> collect(Iterators.product([-1, 1], [-1, 1], [-1, 1]))
8-element Array{Tuple{Int64,Int64,Int64},1}:
(-1,-1,-1)
(1,-1,-1)
(-1,1,-1)
(1,1,-1)
(-1,-1,1)
(1,-1,1)
(-1,1,1)
(1,1,1)
How to do when the dimension is not known a priori?
background: in Python this is achieved with list(itertools.product([-1, 1], repeat=n))
, or even using tuple unpacking into arguments list as in list(itertools.product(*([-1, 1] for i in range(n))))
.