Skip to content

Commit

Permalink
Merge pull request #1028 from hntd187/withFilter
Browse files Browse the repository at this point in the history
Added withFilter for OptionT
  • Loading branch information
non committed May 14, 2016
2 parents c3a2326 + 2869e7f commit 3febb06
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion core/src/main/scala/cats/data/OptionT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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)))

Expand Down
6 changes: 6 additions & 0 deletions tests/src/test/scala/cats/tests/OptionTTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3febb06

Please sign in to comment.