Monad error instance for recovering errors in F instead of the underlying Either.
Monad error instance for recovering errors in F instead of the underlying Either.
scala> import cats.data.EitherT scala> import cats.MonadError scala> import cats.instances.option._ scala> val noInt: Option[Either[String, Int]] = None scala> val et = EitherT[Option, String, Int](noInt) scala> val me = MonadError[EitherT[Option, String, ?], Unit] scala> me.recover(et) { case () => 1 } res0: cats.data.EitherT[Option,String,Int] = EitherT(Some(Right(1)))
If the condition is satisfied, return the given A
in Right
lifted into the specified Applicative
, otherwise, return the
given E
in Left
lifted into the specified Applicative
.
If the condition is satisfied, return the given A
in Right
lifted into the specified Applicative
, otherwise, return the
given E
in Left
lifted into the specified Applicative
.
scala> import cats.Id scala> import cats.data.EitherT scala> val userInput = "hello world" scala> EitherT.cond[Id]( | userInput.forall(_.isDigit) && userInput.size == 10, | userInput, | "The input does not look like a phone number") res0: EitherT[Id, String, String] = EitherT(Left(The input does not look like a phone number))
Transforms an Either
into an EitherT
, lifted into the specified Applicative
.
Transforms an Either
into an EitherT
, lifted into the specified Applicative
.
Note: The return type is a FromEitherPartiallyApplied[F], which has an apply method on it, allowing you to call fromEither like this:
scala> import cats.implicits._ scala> val t: Either[String, Int] = Either.right(3) scala> EitherT.fromEither[Option](t) res0: EitherT[Option, String, Int] = EitherT(Some(Right(3)))
The reason for the indirection is to emulate currying type parameters.
Transforms an Option
into an EitherT
, lifted into the specified Applicative
and using
the second argument if the Option
is a None
.
Transforms an Option
into an EitherT
, lifted into the specified Applicative
and using
the second argument if the Option
is a None
.
scala> import cats.implicits._ scala> val o: Option[Int] = None scala> EitherT.fromOption[List](o, "Answer not known.") res0: EitherT[List, String, Int] = EitherT(List(Left(Answer not known.))) scala> EitherT.fromOption[List](Some(42), "Answer not known.") res1: EitherT[List, String, Int] = EitherT(List(Right(42)))
Transforms an F[Option]
into an EitherT
, using the second argument if the Option
is a None
.
Transforms an F[Option]
into an EitherT
, using the second argument if the Option
is a None
.
scala> import cats.implicits._ scala> val o: Option[Int] = None scala> EitherT.fromOptionF(List(o), "Answer not known.") res0: EitherT[List, String, Int] = EitherT(List(Left(Answer not known.))) scala> EitherT.fromOptionF(List(Option(42)), "Answer not known.") res1: EitherT[List, String, Int] = EitherT(List(Right(42)))
Creates a left version of EitherT[F, A, B]
from a F[A]
Creates a left version of EitherT[F, A, B]
from a F[A]
scala> import cats.data.EitherT scala> import cats.implicits._ scala> EitherT.left[Int](Option("err")) res0: cats.data.EitherT[Option,String,Int] = EitherT(Some(Left(err)))
Creates a left version of EitherT[F, A, B]
from a A
Creates a left version of EitherT[F, A, B]
from a A
scala> import cats.data.EitherT scala> import cats.implicits._ scala> EitherT.leftT[Option, Int]("err") res0: cats.data.EitherT[Option,String,Int] = EitherT(Some(Left(err)))
Alias for right
Alias for right
scala> import cats.data.EitherT scala> import cats.implicits._ scala> val o: Option[Int] = Some(3) scala> val n: Option[Int] = None scala> EitherT.liftF(o) res0: cats.data.EitherT[Option,Nothing,Int] = EitherT(Some(Right(3))) scala> EitherT.liftF(n) res1: cats.data.EitherT[Option,Nothing,Int] = EitherT(None)
Same as liftF, but expressed as a FunctionK for use with mapK
Same as liftF, but expressed as a FunctionK for use with mapK
scala> import cats._, data._, implicits._ scala> val a: OptionT[Eval, Int] = 1.pure[OptionT[Eval, ?]] scala> val b: OptionT[EitherT[Eval, String, ?], Int] = a.mapK(EitherT.liftK) scala> b.value.value.value res0: Either[String,Option[Int]] = Right(Some(1))
Creates a new EitherT[F, A, B]
from a B
Creates a new EitherT[F, A, B]
from a B
scala> import cats.data.EitherT scala> import cats.implicits._ scala> EitherT.pure[Option, String](3) res0: cats.data.EitherT[Option,String,Int] = EitherT(Some(Right(3)))
Creates a right version of EitherT[F, A, B]
from a F[B]
Creates a right version of EitherT[F, A, B]
from a F[B]
scala> import cats.data.EitherT scala> import cats.implicits._ scala> EitherT.right[String](Option(3)) res0: cats.data.EitherT[Option,String,Int] = EitherT(Some(Right(3)))
Alias for pure
Alias for pure
scala> import cats.data.EitherT scala> import cats.implicits._ scala> EitherT.rightT[Option, String](3) res0: cats.data.EitherT[Option,String,Int] = EitherT(Some(Right(3)))