title: Abort early in a fold
body: <p>What's the best way to terminate a fold early? As a simplified example, imagine I want to sum up the numbers in an <code>Iterable</code>, but if I encounter something I'm not expecting (say an odd number) I might want to terminate. This is a first approximation</p>
  
  <pre><code>def sumEvenNumbers(nums: Iterable[Int]): Option[Int] = {
    nums.foldLeft (Some(0): Option[Int]) {
      case (Some(s), n) if n % 2 == 0 =&gt; Some(s + n)
      case _ =&gt; None
    }
  }
  </code></pre>
  
  <p>However, this solution is pretty ugly (as in, if I did a .foreach and a return -- it'd be much cleaner and clearer) and worst of all, it traverses the entire iterable even if it encounters a non-even number.</p>
  
  <p>So what would be the best way to write a fold like this, that terminates early? Should I just go and write this recursively, or is there a more accepted way?</p>