title: Iterate over cartesian product of vectors
body: <p>I have the following nested loop:</p>
  
  <pre><code>for (x in xs) {
      for (y in ys) {
          # Do something with x and y
      }
  }
  </code></pre>
  
  <p>Which I’d like to flatten so I thought of building a Cartesian product of the two vectors <code>xs</code> and <code>ys</code> and iterating over the result. In Python, this would be trivial:</p>
  
  <pre class="lang-python prettyprint-override"><code>for xy in product(xs, ys):
      # x, y = xy[0], xy[1]
  </code></pre>
  
  <p>But in R, the simplest equivalent I’ve found looks daunting:</p>
  
  <pre><code>xys &lt;- expand.grid(xs, ys)
  for (i in 1 : nrow(xys)) {
      xy &lt;- as.vector(xys[i, ])
      # x &lt;- xy[1], y &lt;- xy[2]
  }
  </code></pre>
  
  <p>Surely there must be a better way, no? (To clarify, I don’t want to iterate over an <em>index</em> … I think there must be a way to directly iterate over the tuples in the product.)</p>