Created
December 15, 2017 06:33
-
-
Save Jacoby6000/4c690ca612daa12acc4d51994f2b912a to your computer and use it in GitHub Desktop.
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
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