Skip to content
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

feat: +EitherT.liftRedeemK #4441

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions core/src/main/scala/cats/data/EitherT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,28 @@ object EitherT extends EitherTInstances {
def apply[A](fa: F[A]): EitherT[F, E, A] = EitherT(F.attempt(fa))
}

/**
* Like [[liftAttemptK]] but gives your chance to map over original error.
*
* {{{
* scala> import cats._, data._, implicits._
* scala> val f: Unit => String = Function.const("panic!")
* scala> val a: Option[Int] = None
* scala> val b: EitherT[Option, String, Int] = EitherT.liftRedeemK[Option, Unit, String](f).apply(a)
* scala> b.value
* res0: Option[Either[String, Int]] = Some(Left(panic!))
*
* scala> val a2: Option[Int] = Some(42)
* scala> val b2: EitherT[Option, String, Int] = EitherT.liftRedeemK[Option, Unit, String](f).apply(a2)
* scala> b2.value
* res1: Option[Either[String, Int]] = Some(Right(42))
* }}}
*/
final def liftRedeemK[F[_], EA, EB](fe: EA => EB)(implicit F: ApplicativeError[F, EA]): F ~> EitherT[F, EB, *] =
new (F ~> EitherT[F, EB, *]) {
def apply[A](fa: F[A]): EitherT[F, EB, A] = EitherT(F.redeem(fa)(ea => Left(fe(ea)), Right.apply))
}

@deprecated("Use EitherT.liftF.", "1.0.0-RC1")
final def liftT[F[_], A, B](fb: F[B])(implicit F: Functor[F]): EitherT[F, A, B] = right(fb)

Expand Down