title: Functional processing of Scala streams without OutOfMemory errors
body: <p>Is it possible to apply functional programming to Scala streams such that the stream is processed sequentially, but the already processed part of the stream can be garbage collected?</p>
  
  <p>For example, I define a <code>Stream</code> that contains the numbers from <code>start</code> to <code>end</code>:</p>
  
  <pre><code>def fromToStream(start: Int, end: Int) : Stream[Int] = {
    if (end &lt; start) Stream.empty
    else start #:: fromToStream(start+1, end)
  }
  </code></pre>
  
  <p>If I sum up the values in a functional style:</p>
  
  <pre><code>println(fromToStream(1,10000000).reduceLeft(_+_))
  </code></pre>
  
  <p>I get an <code>OutOfMemoryError</code> - perhaps since the stackframe of the call to <code>reduceLeft</code> holds a reference to the head of the stream. But if I do this in iterative style, it works:</p>
  
  <pre><code>var sum = 0
  for (i &lt;- fromToStream(1,10000000)) {
    sum += i
  }
  </code></pre>
  
  <p>Is there a way to do this in a functional style without getting an <code>OutOfMemory</code>?</p>
  
  <p><strong>UPDATE</strong>: This was <a href="https://issues.scala-lang.org/browse/SI-2239" rel="nofollow noreferrer">a bug in scala</a> that is fixed now. So this is more or less out of date now.</p>