title: restart iterator on exceptions in Scala
body: <p>I have an iterator (actually a <code>Source.getLines</code>) that's reading an infinite stream of data from a URL. Occasionally the iterator throws a <code>java.io.IOException</code> when there is a connection problem. In such situations, I need to re-connect and re-start the iterator. I want this to be seamless so that the iterator just looks like a normal iterator to the consumer, but underneath is restarting itself as necessary.</p>
  
  <p>For example, I'd like to see the following behavior:</p>
  
  <pre><code>scala&gt; val iter = restartingIterator(() =&gt; new Iterator[Int]{
    var i = -1
    def hasNext = {
      if (this.i &lt; 3) {
        true
      } else {
        throw new IOException
      }
    }
    def next = {
      this.i += 1
      i
    }
  })
  res0: ...
  
  scala&gt; iter.take(6).toList
  res1: List[Int] = List(0, 1, 2, 3, 0, 1)
  </code></pre>
  
  <p>I have a partial solution to this problem, but it will fail on some corner cases (e.g. an IOException on the first item after a restart) and it's pretty ugly:</p>
  
  <pre><code>def restartingIterator[T](getIter: () =&gt; Iterator[T]) = new Iterator[T] {
    var iter = getIter()
    def hasNext = {
      try {
        iter.hasNext
      } catch {
        case e: IOException =&gt; {
          this.iter = getIter()
          iter.hasNext
        }
      }
    }
    def next = {
      try {
        iter.next
      } catch {
        case e: IOException =&gt; {
          this.iter = getIter()
          iter.next
        }
      }
    }
  }
  </code></pre>
  
  <p>I keep feeling like there's a better solution to this, maybe some combination of <code>Iterator.continually</code> and <code>util.control.Exception</code> or something like that, but I couldn't figure one out. Any ideas?</p>