-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Raise#fromEither
and friends
#514
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -79,6 +79,19 @@ trait Raise[F[_], -E] extends Serializable { | |||||||||
def ensure[E2 <: E, A](fa: F[A])(error: => E2)(predicate: A => Boolean)( | ||||||||||
implicit A: Monad[F]): F[A] = | ||||||||||
A.flatMap(fa)(a => if (predicate(a)) A.pure(a) else raise(error)) | ||||||||||
|
||||||||||
def fromEither[A](ea: Either[E, A])(implicit F: Applicative[F]): F[A] = | ||||||||||
ea.fold(raise, F.pure) | ||||||||||
|
||||||||||
def fromEitherT[E2 <: E, A](ea: EitherT[F, E2, A])(implicit F: Monad[F]): F[A] = | ||||||||||
F.flatMap(ea.value)(fromEither(_)) | ||||||||||
|
||||||||||
armanbilge marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
def fromOption[E2 <: E, A](oa: Option[A])(e: => E2)(implicit F: Applicative[F]): F[A] = | ||||||||||
oa.fold[F[A]](raise(e))(F.pure) | ||||||||||
|
||||||||||
def fromOptionT[E2 <: E, A](ota: OptionT[F, A])(e: => E2)(implicit F: Monad[F]): F[A] = | ||||||||||
F.flatMap(ota.value)(oa => fromOption(oa)(e)) | ||||||||||
|
||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we are not accidentally adding a performance penalty comparing with, for example: def fromValidated[E2 <: E, A](va: Validated[E2, A])(implicit F: Applicative[F]): F[A] =
va match {
case Validated.Invalid(e) => raise(e)
case Validated.Valid(a) => F.pure(a)
}
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually we are. Using |
||||||||||
} | ||||||||||
|
||||||||||
private[mtl] trait RaiseMonadPartialOrder[F[_], G[_], E] extends Raise[G, E] { | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Andrew raised a great question about use-cases, which made me realize we can do even better 🙂
For example suppose you have a
Ask[IO, Request[IO]]
and you want to interop with an http4s thingKleisli[IO, Request[IO], Response[IO]]
. Well, now you can!