Stream implementation's filter function overflows

19 views
Skip to first unread message

Prateem Mandal

unread,
Jun 23, 2017, 6:25:23 PM6/23/17
to scala-functional
Hi,

I am implementing lazy list using the example in the book FP in  Scala and I found that it fails when I try to search for prime numbers in fibonacci series. The issue seem to be with filter implementation. If I replace the filter implementation using foldRight with pattern matching, then it seems to work fine.

This is how I am checking for primes on fibonacci

Stream fibs = Stream[BigInt](0, 1) append unfold[BigInt, (BigInt, BigInt)]((0, 1))(_ match { case (a, b) => Some((a + b, (b, a + b))) })
println(fibs.filter(_.isProbablePrime(8)).drop(21).headOption)

Here is my implementation (which is identical to Stream implementation in the book and two filter implementation
1) 'filter' with pattern matching which works and
2) 'filter_overflow' which is like the one in the book and overflows)

Any ideals?

Thanks
Prateem

Prateem Mandal

unread,
Jun 23, 2017, 6:26:19 PM6/23/17
to scala-functional
Adding the gist
Reply all
Reply to author
Forward
0 new messages