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

Add ApplicativeError.redeem and MonadError.redeemWith #2560

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions core/src/main/scala/cats/ApplicativeError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ trait ApplicativeError[F[_], E] extends Applicative[F] {
*/
def attemptT[A](fa: F[A]): EitherT[F, E, A] = EitherT(attempt(fa))

/**
* Returns a new value that transforms the result of the source,
* given the `recover` or `map` functions, which get executed depending
* on whether the result is successful or if it ends in error.
*
* This is an optimization on usage of [[attempt]] and [[map]].
*/
def redeem[A, B](fa: F[A])(recover: E => B, mapper: A => B): F[B] =
map(attempt(fa))(_.fold(recover, mapper))

/**
* Recover from certain errors by mapping them to an `A` value.
*
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/scala/cats/MonadError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ trait MonadError[F[_], E] extends ApplicativeError[F, E] with Monad[F] {
def adaptError[A](fa: F[A])(pf: PartialFunction[E, E]): F[A] =
flatMap(attempt(fa))(_.fold(e => raiseError(pf.applyOrElse[E, E](e, _ => e)), pure))

/**
* Returns a new value that transforms the result of the source,
* given the `recover` or `bind` functions, which get executed depending
* on whether the result is successful or if it ends in error.
*
* This is an optimization on usage of [[attempt]] and [[flatMap]].
*/
def redeemWith[A, B](fa: F[A])(recover: E => F[B], bind: A => F[B]): F[B] =
flatMap(attempt(fa))(_.fold(recover, bind))

/**
* Inverse of `attempt`
*
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/syntax/applicativeError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ final class ApplicativeErrorOps[F[_], E, A](val fa: F[A]) extends AnyVal {
def attemptT(implicit F: ApplicativeError[F, E]): EitherT[F, E, A] =
F.attemptT(fa)

def redeem[B](recover: E => B, map: A => B)(implicit F: ApplicativeError[F, E]): F[B] =
F.redeem(fa)(recover, map)

def recover(pf: PartialFunction[E, A])(implicit F: ApplicativeError[F, E]): F[A] =
F.recover(fa)(pf)

Expand Down
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/syntax/monadError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ final class MonadErrorOps[F[_], E, A](val fa: F[A]) extends AnyVal {

def adaptError(pf: PartialFunction[E, E])(implicit F: MonadError[F, E]): F[A] =
F.adaptError(fa)(pf)

def redeemWith[B](recover: E => F[B], bind: A => F[B])(implicit F: MonadError[F, E]): F[B] =
F.redeemWith(fa)(recover, bind)
}

final class MonadErrorRethrowOps[F[_], E, A](val fea: F[Either[E, A]]) extends AnyVal {
Expand Down