diff --git a/core/src/main/scala/cats/data/XorT.scala b/core/src/main/scala/cats/data/XorT.scala index b50282cfe3..edf75190e2 100644 --- a/core/src/main/scala/cats/data/XorT.scala +++ b/core/src/main/scala/cats/data/XorT.scala @@ -47,6 +47,8 @@ final case class XorT[F[_], A, B](value: F[A Xor B]) { } }) + def valueOr[BB >: B](f: A => BB)(implicit F: Functor[F]): F[BB] = fold(f, identity) + def forall(f: B => Boolean)(implicit F: Functor[F]): F[Boolean] = F.map(value)(_.forall(f)) def exists(f: B => Boolean)(implicit F: Functor[F]): F[Boolean] = F.map(value)(_.exists(f)) diff --git a/tests/src/test/scala/cats/tests/XorTTests.scala b/tests/src/test/scala/cats/tests/XorTTests.scala index c24f1561ff..419036405f 100644 --- a/tests/src/test/scala/cats/tests/XorTTests.scala +++ b/tests/src/test/scala/cats/tests/XorTTests.scala @@ -153,6 +153,12 @@ class XorTTests extends CatsSuite { } } + test("valueOr with Id consistent with Xor valueOr") { + forAll { (xort: XorT[Id, String, Int], f: String => Int) => + xort.valueOr(f) should === (xort.value.valueOr(f)) + } + } + test("getOrElse with Id consistent with Xor getOrElse") { forAll { (xort: XorT[Id, String, Int], i: Int) => xort.getOrElse(i) should === (xort.value.getOrElse(i))