title: N-Dimensional Cartesian Product of a set in Julia
body: <p>I would like to see if there is a more elegant/eficient/julian way to produce a function that takes an iterable and a integer and returns the Cartesian or exterior product of the iterable with itself the specified number of times. 
  In my opinion, the desirable way would be to produce an array of arrays, or a tuple of arrays, or a set of arrays, but if that is not the proper way to do it then I would like to understand why. I have come with the following function, which, to my eyes, looks overly "high-level". This is what I made:</p>
  
  <pre><code>using Base.Iterators
  
  function ncartprod(ss, n)
     argumento=fill(ss, n)
     result=collect(product(argumento...))
     return(result)
  end
  </code></pre>
  
  <p>The function returns an object with a strange type: a n-dimensional array of n-sized tuples... It makes sense for locating the elements,... but then, I could then simply produce one by one.</p>
  
  <p>My original sets, here named ss, are simply arrays of integers, like this:</p>
  
  <pre><code>ss=[-1,0,1]
  </code></pre>
  
  <p>So I can make such things:</p>
  
  <pre><code>r=ncartprod(ss, 3)
  </code></pre>
  
  <p>and obtain something like this:</p>
  
  <pre><code>3×3×3 Array{Tuple{Int64,Int64,Int64},3}:
  [:, :, 1] =
   (-1, -1, -1)  (-1, 0, -1)  (-1, 1, -1)
   (0, -1, -1)   (0, 0, -1)   (0, 1, -1) 
   (1, -1, -1)   (1, 0, -1)   (1, 1, -1) 
  
  [:, :, 2] =
   (-1, -1, 0)  (-1, 0, 0)  (-1, 1, 0)
   (0, -1, 0)   (0, 0, 0)   (0, 1, 0) 
   (1, -1, 0)   (1, 0, 0)   (1, 1, 0) 
  
  [:, :, 3] =
   (-1, -1, 1)  (-1, 0, 1)  (-1, 1, 1)
   (0, -1, 1)   (0, 0, 1)   (0, 1, 1) 
   (1, -1, 1)   (1, 0, 1)   (1, 1, 1) 
  </code></pre>
  
  <p>Is there a nicer, more efficient way?</p>