-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add syntax to create a XorT from an F[A] and from a Xor.
- Loading branch information
1 parent
caf9901
commit 462d3e8
Showing
6 changed files
with
76 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,30 @@ | ||
package cats | ||
package syntax | ||
|
||
import cats.data.Xor | ||
import cats.data.{Xor, XorT} | ||
|
||
trait XorSyntax { | ||
implicit def catsSyntaxXorId[A](a: A): XorIdOps[A] = new XorIdOps(a) | ||
|
||
implicit def catsSyntaxXorXorT[A, B](axb: A Xor B): XorXorTOps[A, B] = new XorXorTOps(axb) | ||
} | ||
|
||
final class XorIdOps[A](val a: A) extends AnyVal { | ||
def left[B]: A Xor B = Xor.Left(a) | ||
def right[B]: B Xor A = Xor.Right(a) | ||
} | ||
|
||
final class XorXorTOps[A, B](val axb: A Xor B) { | ||
/** | ||
* Transform this `Xor` into a [[cats.data.XorT]] while lifting it into the specified Applicative. | ||
* | ||
* {{{ | ||
* scala> import cats.implicits._ | ||
* scala> import cats.data.Xor | ||
* scala> val x: Xor[String, Int] = Xor.right(3) | ||
* scala> x.toXorT2[Option] | ||
* res0: cats.data.XorT[Option, String, Int] = XorT(Some(Right(3))) | ||
* }}} | ||
*/ | ||
def toXorT2[F[_]](implicit F: Applicative[F]): XorT[F, A, B] = XorT(F.pure(axb)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package cats | ||
package syntax | ||
|
||
import cats.data.XorT | ||
|
||
trait XorTSyntax extends XorTSyntax1 { | ||
implicit def catsSyntaxXorT[F[_]: Functor, A](fa: F[A]): XorTOps[F, A] = new XorTOps(fa) | ||
} | ||
|
||
trait XorTSyntax1 { | ||
implicit def catsSyntaxUXorT[FA](fa: FA)(implicit U: Unapply[Functor, FA]): XorTOps[U.M, U.A] = | ||
new XorTOps[U.M, U.A](U.subst(fa))(U.TC) | ||
} | ||
|
||
final class XorTOps[F[_]: Functor, A](val fa: F[A]) { | ||
/** | ||
* Lift this `F[A]` into a `XorT[F, A, R]`. | ||
* | ||
* {{{ | ||
* scala> import cats.implicits._ | ||
* scala> val oa: Option[String] = Some("boo") | ||
* scala> oa.leftXorT[Int] | ||
* res0: cats.data.XorT[Option,String,Int] = XorT(Some(Left(boo))) | ||
* }}} | ||
*/ | ||
def leftXorT[R]: XorT[F, A, R] = XorT.left(fa) | ||
|
||
/** | ||
* Lift this `F[A]` into a `XorT[F, L, A]`. | ||
* | ||
* {{{ | ||
* scala> import cats.implicits._ | ||
* scala> val oa: Option[Int] = Some(1) | ||
* scala> oa.rightXorT[String] | ||
* res0: cats.data.XorT[Option,String,Int] = XorT(Some(Right(1))) | ||
* }}} | ||
*/ | ||
def rightXorT[L]: XorT[F, L, A] = XorT.right(fa) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters