title: Scala: Best way to filter & map in one iteration
body: <p>I'm new to Scala and trying to figure out the best way to filter &amp; map a collection. Here's a toy example to explain my problem.</p>
  
  <p><strong>Approach 1:</strong> This is pretty bad since I'm iterating through the list twice and calculating the same value in each iteration.</p>
  
  <pre><code>val N = 5
  val nums = 0 until 10
  val sqNumsLargerThanN = nums filter { x: Int =&gt; (x * x) &gt; N } map { x: Int =&gt; (x * x).toString }
  </code></pre>
  
  <p><strong>Approach 2:</strong> This is slightly better but I still need to calculate <code>(x * x)</code> twice.</p>
  
  <pre><code>val N = 5
  val nums = 0 until 10
  val sqNumsLargerThanN = nums collect { case x: Int if (x * x) &gt; N =&gt; (x * x).toString }
  </code></pre>
  
  <p>So, is it possible to calculate this without iterating through the collection twice and avoid repeating the same calculations?</p>