Skip to content

Instantly share code, notes, and snippets.

@Jacoby6000
Created December 15, 2017 06:33
Show Gist options
  • Save Jacoby6000/4c690ca612daa12acc4d51994f2b912a to your computer and use it in GitHub Desktop.
Save Jacoby6000/4c690ca612daa12acc4d51994f2b912a to your computer and use it in GitHub Desktop.
class MoreMonadErrorOps[F[_], A](val fa: F[A]) extends AnyVal {
def liftError[G[_], C, D, E](nt: F ~> G)(raiseErr: C => G[E])(implicit G: MonadError[G, E], ev: A <~< Validation[C, D]): G[D] =
for {
a <- nt(fa)
validation = ev(a)
result <- validation.fold(raiseErr(_).flatMap(G.raiseError[D]), G.pure(_))
} yield result
def liftEmpty[G[_], C, E](nt: F ~> G)(raiseErr: => G[E])(implicit G: MonadError[G, E], ev: A <~< Maybe[C]): G[C] =
for {
a <- nt(fa)
maybe = ev(a)
result <- maybe.getOrElseF(raiseErr.flatMap(G.raiseError[C]))
} yield result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment