Skip to content

Instantly share code, notes, and snippets.

@jdegoes jdegoes/mini-zio.scala
Last active Feb 29, 2020

Embed
What would you like to do?
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)
}
}
}
@pierangeloc

This comment has been minimized.

Copy link

pierangeloc commented Nov 18, 2019

to adhere better to the original, shouldn't access be environment ?

@jdegoes

This comment has been minimized.

Copy link
Owner Author

jdegoes commented Nov 18, 2019

@pierangeloc Done, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.