Skip to content

Commit

Permalink
Add getOrElseF method to XorT
Browse files Browse the repository at this point in the history
  • Loading branch information
easel committed Dec 1, 2015
1 parent 8185d3c commit 793b3e7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/src/main/scala/cats/data/XorT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ final case class XorT[F[_], A, B](value: F[A Xor B]) {

def getOrElse[BB >: B](default: => BB)(implicit F: Functor[F]): F[BB] = F.map(value)(_.getOrElse(default))

def getOrElseF[BB >: B](default: => F[BB])(implicit F: Monad[F]): F[BB] = {
F.flatMap(value) {
case Xor.Left(a) => default
case Xor.Right(a) => F.pure(a)
}
}

def recover(pf: PartialFunction[A, B])(implicit F: Functor[F]): XorT[F, A, B] =
XorT(F.map(value)(_.recover(pf)))

Expand Down
6 changes: 6 additions & 0 deletions tests/src/test/scala/cats/tests/XorTTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ class XorTTests extends CatsSuite {
}
}

test("getOrElseF with Id consistent with Xor getOrElse") {
forAll { (xort: XorT[Id, String, Int], i: Int) =>
xort.getOrElseF(i) should === (xort.value.getOrElse(i))
}
}

test("forall with Id consistent with Xor forall") {
forAll { (xort: XorT[Id, String, Int], f: Int => Boolean) =>
xort.forall(f) should === (xort.value.forall(f))
Expand Down

0 comments on commit 793b3e7

Please sign in to comment.