Skip to content

Instantly share code, notes, and snippets.

@debasishg
Created September 28, 2022 15:44
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 debasishg/c86442189e29132841b7e274181d2506 to your computer and use it in GitHub Desktop.
Save debasishg/c86442189e29132841b7e274181d2506 to your computer and use it in GitHub Desktop.
ZIO Kleisli
package zio
case class ZKleisli[-R, +E, -A, +B](
run: A => ZIO[R, E, B]
) { self =>
// compose with a ZIO not lifted into a Kleisli
def mapZIO[R1 <: R, E1 >: E, A1 <: A, C1](bc: B => ZIO[R1, E1, C1]): ZKleisli[R1, E1, A, C1] =
ZKleisli(self.run(_).flatMap(bc))
// Modify the output of the Kleisli function with `f`
def map[C](f: B => C): ZKleisli[R, E, A, C] = ZKleisli(self.run(_).map(f))
// arrow composition tip to tail
def andThen[R1 <: R, E1 >: E, C](
f: ZKleisli[R1, E1, B, C]
): ZKleisli[R1, E1, A, C] =
ZKleisli(self.run(_).flatMap(f.run(_)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment