-
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
Conversation
def fromKleisli[A](ka: Kleisli[F, E, A])(implicit F: FlatMap[F]): F[A] = | ||
ask.flatMap(ka.run(_)) |
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 thing Kleisli[IO, Request[IO], Response[IO]]
. Well, now you can!
As one user of this library, I'd like to "bias towards action" and see you merge this @armanbilge, and some version of the error submarine if you see fit. I think currently the risks of stagnation exceed the risks from lightly reviewed code getting merged, so my preference is for low barriers to contribution, for non-breaking changes at least. |
@benhutchison I literally cannot merge my own PRs :) I can however review and merge work submitted by others. |
Co-authored-by: Igor Ramazanov <igor.ramazanov@protonmail.com>
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
def fromValidated[E2 <: E, A](va: Validated[E2, A])(implicit F: Applicative[F]): F[A] = | |
va.fold(raise, F.pure) | |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Actually we are. Using fold
requires allocating lambdas, pattern matching does not. I don't think it's important in practice.
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.
Bike-shedding a bit, the from
prefix threw me for quite a while as I was trying to parse this. I can't think of a better name though. This is absolutely a nice addition.
Inspired by discussion with @benhutchison in typelevel/cats-effect#3765 (reply in thread). The idea is to add a bunch of methods for lifting from the "primitive" monads
Either
,Reader
,Writer
,State
intoF[_]
. This complements #489 which pushes towards usingIO
forF[_]
instead of the monad transformersEitherT
,Kleisli
,WriterT
, andStateT
. So we need a mechanism for lifting.Ask#fromReader
Raise#fromEither
(analogous toApplicativeError#fromEither
)Stateful#fromState
Tell#fromWriter