Similar to Xor.combine but mapped over an F
context.
Similar to Xor.combine but mapped over an F
context.
Examples:
scala> import cats.data.XorT scala> import cats.implicits._ scala> val l1: XorT[Option, String, Int] = XorT.left(Some("error 1")) scala> val l2: XorT[Option, String, Int] = XorT.left(Some("error 2")) scala> val r3: XorT[Option, String, Int] = XorT.right(Some(3)) scala> val r4: XorT[Option, String, Int] = XorT.right(Some(4)) scala> val noneXorT: XorT[Option, String, Int] = XorT.left(None) scala> l1 combine l2 res0: XorT[Option, String, Int] = XorT(Some(Left(error 1))) scala> l1 combine r3 res1: XorT[Option, String, Int] = XorT(Some(Left(error 1))) scala> r3 combine l1 res2: XorT[Option, String, Int] = XorT(Some(Left(error 1))) scala> r3 combine r4 res3: XorT[Option, String, Int] = XorT(Some(Right(7))) scala> l1 combine noneXorT res4: XorT[Option, String, Int] = XorT(None) scala> noneXorT combine l1 res5: XorT[Option, String, Int] = XorT(None) scala> r3 combine noneXorT res6: XorT[Option, String, Int] = XorT(None) scala> noneXorT combine r4 res7: XorT[Option, String, Int] = XorT(None)
Run this value as a Validated
against the function and convert it back to an XorT
.
Run this value as a Validated
against the function and convert it back to an XorT
.
The Applicative instance for XorT
"fails fast" - it is often useful to "momentarily" have
it accumulate errors instead, which is what the Validated
data type gives us.
Example:
scala> import cats.std.option._ scala> import cats.std.list._ scala> import cats.syntax.cartesian._ scala> type Error = String scala> val v1: Validated[NonEmptyList[Error], Int] = Validated.Invalid(NonEmptyList("error 1")) scala> val v2: Validated[NonEmptyList[Error], Int] = Validated.Invalid(NonEmptyList("error 2")) scala> val xort: XorT[Option, Error, Int] = XorT(Some(Xor.left("error 3"))) scala> xort.withValidated { v3 => (v1 |@| v2 |@| v3.leftMap(NonEmptyList(_))).map{ case (i, j, k) => i + j + k } } res0: XorT[Option, NonEmptyList[Error], Int] = XorT(Some(Left(OneAnd(error 1,List(error 2, error 3)))))
Transformer for
Xor
, allowing the effect of an arbitrary type constructorF
to be combined with the fail-fast effect ofXor
.XorT[F, A, B]
wraps a value of typeF[A Xor B]
. AnF[C]
can be lifted in toXorT[F, A, C]
viaXorT.right
, and lifted in to aXorT[F, C, B]
viaXorT.left
.