From 815b8fb94246b480b1356a8199396b89f69e4e3e Mon Sep 17 00:00:00 2001 From: Cody Allen Date: Tue, 12 Jan 2016 08:12:33 -0500 Subject: [PATCH] Fix StreamingT.filter bug As reported by @liff [here](https://gitter.im/non/cats?at=5694ce218fdd3c0c382e20f1). --- core/src/main/scala/cats/data/StreamingT.scala | 4 +++- tests/src/test/scala/cats/tests/StreamingTTests.scala | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/core/src/main/scala/cats/data/StreamingT.scala b/core/src/main/scala/cats/data/StreamingT.scala index 3c104fd194..214a0c8021 100644 --- a/core/src/main/scala/cats/data/StreamingT.scala +++ b/core/src/main/scala/cats/data/StreamingT.scala @@ -70,7 +70,9 @@ sealed abstract class StreamingT[F[_], A] extends Product with Serializable { lh */ def filter(f: A => Boolean)(implicit ev: Functor[F]): StreamingT[F, A] = this match { - case Cons(a, ft) => if (f(a)) this else Wait(ft.map(_.filter(f))) + case Cons(a, ft) => + val tail = ft.map(_.filter(f)) + if (f(a)) Cons(a, tail) else Wait(tail) case Wait(ft) => Wait(ft.map(_.filter(f))) case Empty() => this } diff --git a/tests/src/test/scala/cats/tests/StreamingTTests.scala b/tests/src/test/scala/cats/tests/StreamingTTests.scala index 753bdae828..7140b9d496 100644 --- a/tests/src/test/scala/cats/tests/StreamingTTests.scala +++ b/tests/src/test/scala/cats/tests/StreamingTTests.scala @@ -78,6 +78,11 @@ class StreamingTTests extends CatsSuite { } } + test("filter - check regression") { + val s = StreamingT[Option, Int](1, 2, 1) + s.filter(_ > 1).toList should === (Some(List(2))) + } + test("foldLeft with Id consistent with List.foldLeft") { forAll { (s: StreamingT[Id, Int], l: Long, f: (Long, Int) => Long) => s.foldLeft(l)(f) should === (s.toList.foldLeft(l)(f))