-
-
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 d0d8fe0
Showing
5 changed files
with
77 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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package cats | ||
package syntax | ||
|
||
import cats.data.{Xor, XorT} | ||
|
||
trait XorTSyntax extends XorTSyntax1 { | ||
implicit def catsSyntaxXorT[F[_]: Functor, A](fa: F[A]): XorTOps[F, A] = new XorTOps(fa) | ||
|
||
implicit def catsSyntaxXorTXor[A, B](axb: A Xor B): XorTXorOps[A, B] = new XorTXorOps(axb) | ||
} | ||
|
||
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) | ||
} | ||
|
||
|
||
final class XorTXorOps[A, B](val axb: A Xor B) { | ||
/** | ||
* Transform this `Xor` into a [[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