Skip to content

Commit

Permalink
Merge pull request #705 from easel/xort-getorelsef
Browse files Browse the repository at this point in the history
Add getOrElseF method to XorT
  • Loading branch information
ceedubs committed Dec 1, 2015
2 parents 399f645 + ec534f6 commit 0fcefbf
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(_) => default
case Xor.Right(b) => F.pure(b)
}
}

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 0fcefbf

Please sign in to comment.