title: Returning values which compute sum
body: <p>Here is a Scala stream to calculate the Fibonacci sequence : </p>
  
  <pre><code>    import scala.math.BigInt
  
      object fib extends App {
  
        val fibs: Stream[BigInt] = BigInt(0) #:: BigInt(1) #:: fibs.zip(
          fibs.tail).map(n =&gt; {
            n._1 + n._2
          })
      }
    fibs take 5 foreach println
    fibs take 6 foreach println
  </code></pre>
  
  <p>src : <a href="http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Stream" rel="nofollow">http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Stream</a></p>
  
  <p>Instead of taking the first n numbers in the sequence how to sum the first n numbers using a stream , and return this sum ?</p>
  
  <p>I could introduce a var <code>sum</code> like this : </p>
  
  <pre><code>object fib extends App {
  
      var sum = 3;
    val fibs: Stream[BigInt] = BigInt(0) #:: BigInt(1) #:: fibs.zip(fibs.tail).map(n =&gt; {
  
        sum = n._1.toInt + n._2.toInt
        n._1 + n._2
  
      })
  
    fibs take 5 foreach println
  
  }
  </code></pre>
  
  <p>And then somehow check this summed value. But this is not a good solution.</p>
  
  <p>Update : I'm attempting to provide the summed value and return values that compute this sum, not sum the actual stream.</p>
  
  <p>So <code>takeSum(7)</code> will return <code>'0,1,1,2,3'</code></p>
  
  <p>Assumption is that the n value sum will contain a subset of the fib sequence</p>