Created
March 15, 2022 10:15
-
-
Save telekosmos/9837185cd255063ebf8805c494bf3523 to your computer and use it in GitHub Desktop.
EitherT for comprehensions
This file contains hidden or 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
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 |
This file contains hidden or 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
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