title: Idiomatic Scala solution to imperative code
body: <p>What are some ideas for expressing this function in 'idiomatic' Scala. Or more precisely, is there a way to remove the local vars without sacrificing readability?</p>
  
  <pre><code>def solve(threshold: Int)(f: Int =&gt; Int): Int = {
    var sum = 0
    var curr = 0
    while(sum &lt; threshold) {
     sum += f(curr)
     curr += 1
    }
    curr
  }
  </code></pre>
  
  <p>The only thing I could come up with was this, but it's longer and less readable in my opinion.</p>
  
  <pre><code>def solve2(threshold: Int)(f: Int =&gt; Int): Int = {
    val resultIterator = Iterator.iterate (0, 0) { case (curr, sum) =&gt;
      (curr + 1, sum + f(curr))
    }
    (resultIterator find (_._2 &gt;= threshold)).get._1
  }
  </code></pre>