Skip to content

Instantly share code, notes, and snippets.

@telekosmos
Created March 15, 2022 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telekosmos/9837185cd255063ebf8805c494bf3523 to your computer and use it in GitHub Desktop.
Save telekosmos/9837185cd255063ebf8805c494bf3523 to your computer and use it in GitHub Desktop.
EitherT for comprehensions
import cats._
import cats.implicits._
import cats.effect._
import cats.data.EitherT
val res: EitherT[IO, Throwable, Unit] = for {
one <- EitherT[IO, Throwable, Int](IO(1.asRight[Throwable]))
// e <- EitherT(IO((new RuntimeException).asLeft[Int]))
two <- EitherT(IO(2.asRight[Throwable]))
} yield println(one + two)
val prog = IO { println(s"** Before ** ${Instant.now().toString}") } >> res.value >> IO { println(s"** After ** ${Instant.now().toString}") }
// val prog: cats.effect.IO[Unit] = IO(...)
prog.unsafeRunSync
** Before ** 2032-02-15T10:12:41.495Z
** After ** 2032-02-15T10:12:41.495Z
import cats._
import cats.implicits._
import cats.effect._
import cats.data.EitherT
val res: EitherT[IO, Throwable, Unit] = for {
one <- EitherT[IO, Throwable, Int](IO(1.asRight[Throwable]))
// e <- EitherT(IO((new RuntimeException).asLeft[Int]))
two <- EitherT(IO(2.asRight[Throwable]))
} yield println(one + two)
val prog = IO { println(s"** Before ** ${Instant.now().toString}") } >> res.value >> IO { println(s"** After ** ${Instant.now().toString}") }
// val prog: cats.effect.IO[Unit] = IO(...)
prog.unsafeRunSync
** Before ** 2032-02-15T10:12:41.495Z
3
** After ** 2032-02-15T10:12:41.495Z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment