You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Chapter 3.3, Exercise: Stream Combinators, the provided implementation of the filter method for the Stream trait seems incorrect.
In the current implementation, the tail method doesn’t correctly apply the filtering logic. It simply calls stream.tail, which doesn’t skip non-matching elements, causing infinite recursion or incorrect results.
Current Implementation:
def filter(pred: A => Boolean): Stream[A] = {
val self = this
new Stream[A] {
def head: A = {
def loop(stream: Stream[A]): A =
if pred(stream.head) then stream.head
else loop(stream.tail)
loop(self)
}
def tail: Stream[A] = {
def loop(stream: Stream[A]): Stream[A] =
if pred(stream.head) then stream.tail
else loop(stream.tail)
loop(self)
}
}
}
Suggested Fix:
The tail method should continue filtering the stream.
In Chapter 3.3, Exercise: Stream Combinators, the provided implementation of the filter method for the Stream trait seems incorrect.
In the current implementation, the tail method doesn’t correctly apply the filtering logic. It simply calls stream.tail, which doesn’t skip non-matching elements, causing infinite recursion or incorrect results.
Current Implementation:
Suggested Fix:
The tail method should continue filtering the stream.
Thanks for the great book!
The text was updated successfully, but these errors were encountered: