title: Is it advisable to use for comprehensions with yield to return a huge number of items?
body: <p>I am working on a little program that generates combinations and I am using <code>for</code> comprehensions. Something like this:</p>
  
  <pre><code>def posibilities2(n: Int): Seq[List[Int]] = {
    val maxValues = (1 to 3).map(i =&gt; n / i).toList
    for {
      n1 &lt;- 0 to maxValues(0)
      n2 &lt;- 0 to maxValues(1)
      n3 &lt;- 0 to maxValues(2)
      if n1 * 1 + n2 * 2 + n3 * 3  == n
    }
      yield List(n1, n2, n3)
  }  
  
  posibilities2(1000).foreach(doSomething)
  </code></pre>
  
  <p>For bigger values of n it can lead to lots of items.</p>
  
  <p>My question is this: Is this the way to do it, given that, for each item generated, I have to do some additional processing? I am not concerned about the program taking a long time to run, I am concerned about running out of memory. </p>
  
  <p>Thank you</p>