diff --git a/core/src/main/scala/cats/data/Streaming.scala b/core/src/main/scala/cats/data/Streaming.scala index 2a58376e0b..bb61a2e5a3 100644 --- a/core/src/main/scala/cats/data/Streaming.scala +++ b/core/src/main/scala/cats/data/Streaming.scala @@ -501,10 +501,11 @@ sealed abstract class Streaming[A] extends Product with Serializable { lhs => * If no elements satisfy `f`, an empty stream will be returned. * * For example: - * - * Streaming(1, 2, 3, 4, 5, 6, 7).takeWhile(n => n != 4) - * - * Will result in: Streaming(1, 2, 3) + * {{{ + * scala> val s = Streaming(1, 2, 3, 4, 5, 6, 7) + * scala> s.takeWhile(n => n != 4).toList + * res0: List[Int] = List(1, 2, 3) + * }}} */ def takeWhile(f: A => Boolean): Streaming[A] = this match { @@ -523,16 +524,17 @@ sealed abstract class Streaming[A] extends Product with Serializable { lhs => * If no elements satisfy `f`, the current stream will be returned. * * For example: - * - * Streaming(1, 2, 3, 4, 5, 6, 7).takeWhile(n => n != 4) - * - * Will result in: Streaming(4, 5, 6, 7) + * {{{ + * scala> val s = Streaming(1, 2, 3, 4, 5, 6, 7) + * scala> s.dropWhile(n => n != 4).toList + * res0: List[Int] = List(4, 5, 6, 7) + * }}} */ def dropWhile(f: A => Boolean): Streaming[A] = this match { case Empty() => Empty() case Wait(lt) => Wait(lt.map(_.dropWhile(f))) - case Cons(a, lt) => if (f(a)) Empty() else Cons(a, lt.map(_.dropWhile(f))) + case s @ Cons(a, lt) => if (f(a)) Wait(lt.map(_.dropWhile(f))) else s } /** diff --git a/core/src/main/scala/cats/data/StreamingT.scala b/core/src/main/scala/cats/data/StreamingT.scala index 54f46eb1e2..51c972f22e 100644 --- a/core/src/main/scala/cats/data/StreamingT.scala +++ b/core/src/main/scala/cats/data/StreamingT.scala @@ -222,10 +222,12 @@ sealed abstract class StreamingT[F[_], A] extends Product with Serializable { lh * If no elements satisfy `f`, an empty stream will be returned. * * For example: - * - * StreamingT[List, Int](1, 2, 3, 4, 5, 6, 7).takeWhile(n => n != 4) - * - * Will result in: StreamingT[List, Int](1, 2, 3) + * {{{ + * scala> import cats.std.list._ + * scala> val s = StreamingT[List, Int](1, 2, 3, 4, 5, 6, 7) + * scala> s.takeWhile(n => n != 4).toList.flatten + * res0: List[Int] = List(1, 2, 3) + * }}} */ def takeWhile(f: A => Boolean)(implicit ev: Functor[F]): StreamingT[F, A] = this match { @@ -244,14 +246,16 @@ sealed abstract class StreamingT[F[_], A] extends Product with Serializable { lh * If no elements satisfy `f`, the current stream will be returned. * * For example: - * - * StreamingT[List, Int](1, 2, 3, 4, 5, 6, 7).dropWhile(n => n != 4) - * - * Will result in: StreamingT[List, Int](4, 5, 6, 7) + * {{{ + * scala> import cats.std.list._ + * scala> val s = StreamingT[List, Int](1, 2, 3, 4, 5, 6, 7) + * scala> s.dropWhile(n => n != 4).toList.flatten + * res0: List[Int] = List(4, 5, 6, 7) + * }}} */ def dropWhile(f: A => Boolean)(implicit ev: Functor[F]): StreamingT[F, A] = this match { - case Cons(a, ft) => if (f(a)) Empty() else Cons(a, ft.map(_.dropWhile(f))) + case s @ Cons(a, ft) => if (f(a)) Wait(ft.map(_.dropWhile(f))) else s case Wait(ft) => Wait(ft.map(_.dropWhile(f))) case Empty() => Empty() } diff --git a/laws/src/main/scala/cats/laws/discipline/Arbitrary.scala b/laws/src/main/scala/cats/laws/discipline/Arbitrary.scala index ec5da2481b..cb69e6964d 100644 --- a/laws/src/main/scala/cats/laws/discipline/Arbitrary.scala +++ b/laws/src/main/scala/cats/laws/discipline/Arbitrary.scala @@ -11,6 +11,12 @@ import org.scalacheck.Arbitrary.{arbitrary => getArbitrary} */ object arbitrary extends ArbitraryInstances0 { + // A special function1Arbitrary for testing operations like dropWhile specifically + // in the context of Int => Boolean. Once scalacheck supports better Function1 arbitrary + // instances this can be removed. + implicit def function1Arbitrary: Arbitrary[(Int) => Boolean] = + Arbitrary(getArbitrary[Int].map(x => (input) => input < x)) + implicit def constArbitrary[A, B](implicit A: Arbitrary[A]): Arbitrary[Const[A, B]] = Arbitrary(A.arbitrary.map(Const[A, B]))