From 2869e7f42e1fd93169d02527b1bbae80da993c55 Mon Sep 17 00:00:00 2001 From: Stephen Carman Date: Wed, 11 May 2016 11:17:17 -0400 Subject: [PATCH] Added withFilter for OptionT --- core/src/main/scala/cats/data/OptionT.scala | 5 ++++- tests/src/test/scala/cats/tests/OptionTTests.scala | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/core/src/main/scala/cats/data/OptionT.scala b/core/src/main/scala/cats/data/OptionT.scala index 4ab1d31eb7..6db53b4243 100644 --- a/core/src/main/scala/cats/data/OptionT.scala +++ b/core/src/main/scala/cats/data/OptionT.scala @@ -2,7 +2,7 @@ package cats package data /** - * `OptionT[F[_], A` is a light wrapper on an `F[Option[A]]` with some + * `OptionT[F[_], A]` is a light wrapper on an `F[Option[A]]` with some * convenient methods for working with this nested structure. * * It may also be said that `OptionT` is a monad transformer for `Option`. @@ -59,6 +59,9 @@ final case class OptionT[F[_], A](value: F[Option[A]]) { def filter(p: A => Boolean)(implicit F: Functor[F]): OptionT[F, A] = OptionT(F.map(value)(_.filter(p))) + def withFilter(p: A => Boolean)(implicit F: Functor[F]): OptionT[F, A] = + filter(p)(F) + def filterNot(p: A => Boolean)(implicit F: Functor[F]): OptionT[F, A] = OptionT(F.map(value)(_.filterNot(p))) diff --git a/tests/src/test/scala/cats/tests/OptionTTests.scala b/tests/src/test/scala/cats/tests/OptionTTests.scala index 0873a8cff9..95cb4fac7d 100644 --- a/tests/src/test/scala/cats/tests/OptionTTests.scala +++ b/tests/src/test/scala/cats/tests/OptionTTests.scala @@ -50,6 +50,12 @@ class OptionTTests extends CatsSuite { } } + test("OptionT[Id, A].withFilter consistent with Option.withFilter"){ + forAll { (o: Option[Int], f: Int => Boolean) => + (for {x <- o if f(x)} yield x) should === ((for {x <- OptionT[Id, Int](o) if f(x)} yield x).value) + } + } + test("OptionT[Id, A].filterNot consistent with Option.filterNot") { forAll { (o: Option[Int], f: Int => Boolean) => o.filterNot(f) should === (OptionT[Id, Int](o).filterNot(f).value)