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

attemptTap #3459

Merged
merged 5 commits into from
Jul 5, 2020
Merged
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
27 changes: 27 additions & 0 deletions core/src/main/scala/cats/MonadError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,33 @@ trait MonadError[F[_], E] extends ApplicativeError[F, E] with Monad[F] {
def redeemWith[A, B](fa: F[A])(recover: E => F[B], bind: A => F[B]): F[B] =
flatMap(attempt(fa))(_.fold(recover, bind))

/**
* Reifies the value or error of the source and performs an effect on the result,
* then recovers the original value or error back into `F`.
*
* Note that if the effect returned by `f` fails, the resulting effect will fail too.
*
* Alias for `fa.attempt.flatTap(f).rethrow` for convenience.
*
* Example:
* {{{
* scala> import cats.implicits._
* scala> import scala.util.{Try, Success, Failure}
*
* scala> def checkError(result: Either[Throwable, Int]): Try[String] = result.fold(_ => Failure(new java.lang.Exception), _ => Success("success"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a note, but don't consider it a blocker for the PR. I personally like the example in your comment here

fa.attemptTap {
  case Right(a) => log.info(s"succeeded with $a")
  case Left(e) => log.error(s"failed with $e")
}

It describes very well a real use case for attemptTap. A simple program could be simulated with State. Something like

/**
 * {{{
 * scala> import cats._, data._, implicits._
 * scala> type F[A] = EitherT[State[String, *], String, A]
 * scala> object log { def info(m: String): F[Unit] = EitherT.liftF(State.modify(_ => s"info: $m")); def error(m: String): F[Unit] = EitherT.liftF(State.modify(_ => s"error: $m")) }
 *
 * scala> val prog1: F[Int] = EitherT.liftF(State.pure(1))
 * scala> val prog2: F[Int] = "this one failed".raiseError[F, Int]
 *
 * scala> val run1 = prog1.attemptTap { case Right(a) => log.info(a.toString); case Left(e) => log.error(e) }
 * scala> val run2 = prog2.attemptTap { case Right(a) => log.info(a.toString); case Left(e) => log.error(e) }
 *
 * scala> run1.value.runS("init").value
 * res0: String = info: 1
 *
 * scala> run2.value.runS("init").value
 * res1: String = error: this one failed
 * }}}
 */

*
* scala> val a: Try[Int] = Failure(new Throwable("failed"))
* scala> a.attemptTap(checkError)
* res0: scala.util.Try[Int] = Failure(java.lang.Exception)
*
* scala> val b: Try[Int] = Success(1)
* scala> b.attemptTap(checkError)
* res1: scala.util.Try[Int] = Success(1)
* }}}
*/
def attemptTap[A, B](fa: F[A])(f: Either[E, A] => F[B]): F[A] =
rethrow(flatTap(attempt(fa))(f))

override def adaptError[A](fa: F[A])(pf: PartialFunction[E, E]): F[A] =
recoverWith(fa)(pf.andThen(raiseError[A] _))
}
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 @@ -32,6 +32,9 @@ final class MonadErrorOps[F[_], E, A](private val fa: F[A]) extends AnyVal {

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

def attemptTap[B](f: Either[E, A] => F[B])(implicit F: MonadError[F, E]): F[A] =
F.attemptTap(fa)(f)
}

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