Skip to main content

EitherT

Extension Methods for EitherT

import extras.cats.syntax.either._

or

import extras.cats.syntax.all._

eitherT / t for F[Either[A, B]]

When you have fab: F[Either[A, B]], instead of EitherT(fab), you can simply do

fab.eitherT // EitherT[F, A, B]
// or
fab.t // EitherT[F, A, B]
import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val fab = IO.pure(1.asRight[String])
// fab: IO[Either[String, Int]] = Pure(a = Right(value = 1))
fab.t
// res1: cats.data.EitherT[IO, String, Int] = EitherT(
// value = Pure(a = Right(value = 1))
// )

val f = IO(println("Hello").asRight[String])
// f: IO[Either[String, Unit]] = Delay(
// thunk = <function0>,
// trace = StackTrace(
// ...
f.t
// res2: cats.data.EitherT[IO, String, Unit] = EitherT(
// value = Delay(
// thunk = <function0>,
// ...

eitherT / t for Either[A, B]

When you have ab: Either[A, B], instead of EitherT.fromEither[F](ab), you can simply do

ab.eitherT[F] // EitherT[F, A, B]
// or
ab.t[F] // EitherT[F, A, B]
import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val ab = 1.asRight[String]
// ab: Either[String, Int] = Right(value = 1)
ab.t[IO]
// res4: cats.data.EitherT[IO, String, Int] = EitherT(
// value = Pure(a = Right(value = 1))
// )

rightT for F[B]

When you have fb: F[B], instead of EitherT.right[A](fb), you can simply do

fb.rightT[A] // EitherT[F, A, B]
import cats.effect._

import extras.cats.syntax.all._

val fb = IO.pure(1)
// fb: IO[Int] = Pure(a = 1)
fb.rightT[String]
// res6: cats.data.EitherT[IO, String, Int] = EitherT(
// value = Map(
// source = Pure(a = 1),
// ...

val f = IO(println("Hello"))
// f: IO[Unit] = Delay(
// thunk = <function0>,
// trace = StackTrace(
// ...
f.rightT[String]
// res7: cats.data.EitherT[IO, String, Unit] = EitherT(
// value = Map(
// source = Delay(
// ...

leftT for F[A]

When you have fa: F[A], instead of EitherT.left[B](fa), you can simply do

fa.leftT[B] // EitherT[F, A, B]
import cats.effect._

import extras.cats.syntax.all._

val fa = IO.pure("ERROR!!!")
// fa: IO[String] = Pure(a = "ERROR!!!")
fa.leftT[Int]
// res9: cats.data.EitherT[IO, String, Int] = EitherT(
// value = Map(
// source = Pure(a = "ERROR!!!"),
// ...

val f = IO(println("ERROR!!!"))
// f: IO[Unit] = Delay(
// thunk = <function0>,
// trace = StackTrace(
// ...
f.leftT[Int]
// res10: cats.data.EitherT[IO, Unit, Int] = EitherT(
// value = Map(
// source = Delay(
// ...

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

final case class MyError(message: String)

def foo[F[_]: Sync](n: Int): F[Int] = Sync[F].pure(n * 2)

def bar[F[_]: Sync](n: Int): F[Either[MyError, Int]] =
if (n < 0)
Sync[F].pure(MyError(s"n cannot be a negative number. [n: $n]").asLeft)
else
Sync[F].pure((n + 100).asRight)

def divide[F[_]: Sync](a: Int, b: Int): F[Either[MyError, Int]] =
if (b == 0)
MyError(s"You can divide number by 0. [a: $a, b: $b]").asLeft.pure[F]
else
Sync[F].delay((a / b).asRight)

def run[F[_]: Sync](): F[Either[MyError, Int]] = (for {
a <- foo(123).rightT
b <- 2.rightTF[F, MyError]
c <- bar(b).eitherT
d <- divide(a, c).t
} yield d).value

println(run[IO]().unsafeRunSync())
// Right(2)