From 8a18a7345dc2cf2776485da32441b10128488abf Mon Sep 17 00:00:00 2001 From: Olli Helenius Date: Tue, 5 Apr 2016 21:02:21 +0300 Subject: [PATCH] Add toValidatedNel to Xor As a convenient shorthand for xor.toValidated.toValidatedNel. --- core/src/main/scala/cats/data/Xor.scala | 4 ++++ tests/src/test/scala/cats/tests/XorTests.scala | 1 + 2 files changed, 5 insertions(+) diff --git a/core/src/main/scala/cats/data/Xor.scala b/core/src/main/scala/cats/data/Xor.scala index 7b6cc3bf27..470fa448d4 100644 --- a/core/src/main/scala/cats/data/Xor.scala +++ b/core/src/main/scala/cats/data/Xor.scala @@ -70,6 +70,10 @@ sealed abstract class Xor[+A, +B] extends Product with Serializable { def toValidated: Validated[A,B] = fold(Validated.Invalid.apply, Validated.Valid.apply) + /** Returns a [[ValidatedNel]] representation of this disjunction with the `Left` value + * as a single element on the `Invalid` side of the [[NonEmptyList]]. */ + def toValidatedNel[AA >: A]: ValidatedNel[AA,B] = fold(Validated.invalidNel, Validated.valid) + def withValidated[AA,BB](f: Validated[A,B] => Validated[AA,BB]): AA Xor BB = f(toValidated).toXor diff --git a/tests/src/test/scala/cats/tests/XorTests.scala b/tests/src/test/scala/cats/tests/XorTests.scala index adf7cf91e7..58d0a90de2 100644 --- a/tests/src/test/scala/cats/tests/XorTests.scala +++ b/tests/src/test/scala/cats/tests/XorTests.scala @@ -204,6 +204,7 @@ class XorTests extends CatsSuite { x.isLeft should === (x.toOption.isEmpty) x.isLeft should === (x.toList.isEmpty) x.isLeft should === (x.toValidated.isInvalid) + x.isLeft should === (x.toValidatedNel.isInvalid) } }