From 45c0be4f057c7e9868338367e4add1c94d9d04a7 Mon Sep 17 00:00:00 2001 From: Denis Mikhaylov Date: Fri, 15 Jan 2016 11:24:10 +0300 Subject: [PATCH 1/2] Add XorT#valueOr --- core/src/main/scala/cats/data/XorT.scala | 2 ++ 1 file changed, 2 insertions(+) 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)) From 07d0cd1feaea225ba8674c985f9e5c546b5e2a29 Mon Sep 17 00:00:00 2001 From: Denis Mikhaylov Date: Fri, 15 Jan 2016 11:24:19 +0300 Subject: [PATCH 2/2] Add test for XorT#valueOr --- tests/src/test/scala/cats/tests/XorTTests.scala | 6 ++++++ 1 file changed, 6 insertions(+) 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))