Last active
November 22, 2020 14:24
-
-
Save jdegoes/9729a8e50b2fd2f473a4ee9371755134 to your computer and use it in GitHub Desktop.
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
final case class ZIO[-R, +E, +A](run: R => Either[E, A]) { | |
final def map[B](f: A => B): ZIO[R, E, B] = | |
ZIO(r => run(r).map(f)) | |
final def flatMap[R1 <: R, E1 >: E, B](f: A => ZIO[R1, E1, B]): ZIO[R1, E1, B] = | |
ZIO(r => run(r).flatMap(a => f(a).run(r))) | |
final def provide(r: R): ZIO[Any, E, A] = | |
ZIO(_ => run(r)) | |
final def either: ZIO[R, Nothing, Either[E, A]] = | |
ZIO(r => Right(run(r))) | |
} | |
object ZIO { | |
def succeed[A](a: A): ZIO[Any, Nothing, A] = ZIO(_ => Right(a)) | |
def fail[E](e: E): ZIO[Any, E, Nothing] = ZIO(_ => Left(e)) | |
def environment[R]: ZIO[R, Nothing, R] = ZIO(r => Right(r)) | |
def effect[A](action: => A): ZIO[Any, Throwable, A] = ZIO { _ => | |
try Right(action) | |
catch { | |
case t : Throwable => Left(t) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@pierangeloc Done, thank you!