title: Index iteration idiom
body: <p>The following code is commonly seen on SO when it comes to iterating over the index values of a collection:</p>
  
  <pre><code>for (i in 1:length(x)) {
    # ...
  }
  </code></pre>
  
  <p>The code misbehaves when the collection is empty because <code>1:length(x)</code> becomes <code>1:0</code> which gives <code>i</code> the values <code>1</code> and <code>0</code>.</p>
  
  <pre><code>iterate &lt;- function(x) {
      for (i in 1:length(x)) {
        cat('x[[', i, ']] is', x[[i]], '\n')
      }
  }
  
  &gt; iterate(c(1,2,3))
  x[[ 1 ]] is 1 
  x[[ 2 ]] is 2 
  x[[ 3 ]] is 3 
  
  &gt; iterate(c())
  x[[ 1 ]] is  
  x[[ 0 ]] is
  </code></pre>
  
  <p>I recall seeing an elegant idiom for defining a sequence that has no elements when <code>x</code> is empty but I cannot remember it. What idiom do you use?</p>