Re: StackOverFlowError When Applying Filter to a Stream

Simon Schäfer <[email protected]>
Newsgroups gmane.comp.lang.scala
Message-ID <[email protected]>

On 06.08.2015 02:27, [email protected] wrote:
> Could you expand on this bit please:
>
> In the first case there are simply not enough values left to continue 
> the stream.
Well, your Stream contains two values: 0 and 1. They are both accessed 
and when the third value needs to be accessed it needs to be computed. 
The third value is 0+1==1. Given that it is odd, it is skipped by the 
filter. For the fourth value the third one needs to exist, which is not 
the case. Therefore it also can't be computed.

In the second example, the second 1 is not skipped. Instead it is stored 
in the original Stream. The filter only skips values by constructing the 
second Stream but the original Stream is never touched and therefore all 
odd values can skipped without further problems.
>
> On Wednesday, 5 August 2015 19:46:47 UTC+1, Simon Schäfer wrote:
>
>     The first time, the filter is part of fibs, the second time it is
>     part of another stream accessing fibs. In the first case there are
>     simply not enough values left to continue the stream.
>
>     On 05.08.2015 20:16, [email protected] <javascript:> wrote:
>>
>>     Hello,
>>
>>
>>     Please can some one explain why the following code blows the stack:
>>
>>     |
>>     |
>>     |val  fibs:  Stream[BigInt]  =  BigInt(0)  #::  BigInt(1)  #::  fibs.zip(fibs.tail).map{  n=>  n._1+  n._2}.takeWhile(x=>  x<  4000000).filter(x=>  x%  2  ==  0)
>>
>>     scala>  fibs foreach println
>>     0
>>     1
>>     java.lang.StackOverflowError|
>>
>>     If I take out the filter and apply it to the stream in another
>>     expression as follows, it is fine:
>>
>>     |scala>  val  fibs:  Stream[BigInt]  =  BigInt(0)  #::  BigInt(1)  #::  fibs.zip(fibs.tail).map{  n=>  n._1+  n._2}.takeWhile(x=>  x<  4000000)
>>     fibs:  Stream[scala.math.BigInt]  =  Stream(0,  ?)
>>
>>     scala>  fibs filter(  x=>  x%  2  ==  0)
>>     res8:  scala.collection.immutable.Stream[scala.math.BigInt]  =  Stream(0,  ?)
>>
>>     scala>  fibs filter(  x=>  x%  2  ==  0)  foreach println
>>     0
>>     2
>>     8
>>     34
>>     144
>>     610
>>     2584
>>     10946
>>     46368
>>     196418
>>     832040
>>     3524578|
>>
>>     Why does it blow the stack with the first approach but not the
>>     second?
>>
>>
>>     Thanks
>>
>>     Iftikhar
>>
>>     -- 
>>     You received this message because you are subscribed to the
>>     Google Groups "scala-language" group.
>>     To unsubscribe from this group and stop receiving emails from it,
>>     send an email to [email protected] <javascript:>.
>>     For more options, visit https://groups.google.com/d/optout
>>     <https://groups.google.com/d/optout>.
>
> -- 
> You received this message because you are subscribed to the Google 
> Groups "scala-language" group.
> To unsubscribe from this group and stop receiving emails from it, send 
> an email to [email protected] 
> <mailto:[email protected]>.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.