From 9e9d6fd9518cb3cf4ed49008687fc51786821501 Mon Sep 17 00:00:00 2001 From: Alexandru Nedelcu Date: Fri, 24 Apr 2020 17:55:41 +0300 Subject: [PATCH 1/2] Optimize redeem/redeemWith in instances --- .../src/main/scala/cats/effect/IO.scala | 5 ++++ .../src/main/scala/cats/effect/Sync.scala | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/core/shared/src/main/scala/cats/effect/IO.scala b/core/shared/src/main/scala/cats/effect/IO.scala index eae58406cc..517fa312c7 100644 --- a/core/shared/src/main/scala/cats/effect/IO.scala +++ b/core/shared/src/main/scala/cats/effect/IO.scala @@ -883,6 +883,11 @@ abstract private[effect] class IOLowPriorityInstances extends IOParallelNewtype final override def guaranteeCase[A](fa: IO[A])(finalizer: ExitCase[Throwable] => IO[Unit]): IO[A] = fa.guaranteeCase(finalizer) + final override def redeem[A, B](fa: IO[A])(recover: Throwable => B, f: A => B): IO[B] = + fa.redeem(recover, f) + final override def redeemWith[A, B](fa: IO[A])(recover: Throwable => IO[B], bind: A => IO[B]): IO[B] = + fa.redeemWith(recover, bind) + final override def delay[A](thunk: => A): IO[A] = IO.delay(thunk) final override def suspend[A](thunk: => IO[A]): IO[A] = diff --git a/core/shared/src/main/scala/cats/effect/Sync.scala b/core/shared/src/main/scala/cats/effect/Sync.scala index b865d3e834..d098e904ea 100644 --- a/core/shared/src/main/scala/cats/effect/Sync.scala +++ b/core/shared/src/main/scala/cats/effect/Sync.scala @@ -118,6 +118,18 @@ object Sync { def raiseError[A](e: Throwable): EitherT[F, L, A] = EitherT.liftF(F.raiseError(e)) + override def redeem[A, B](fa: EitherT[F, L, A])(recover: Throwable => B, f: A => B): EitherT[F, L, B] = + EitherT(F.redeem(fa.value)(e => Right(recover(e)), la => la.map(f))) + + override def redeemWith[A, B](fa: EitherT[F, L, A])(recover: Throwable => EitherT[F, L, B], bind: A => EitherT[F, L, B]): EitherT[F, L, B] = + EitherT(F.redeemWith(fa.value)( + e => recover(e).value, + la => la match { + case Right(a) => bind(a).value + case _ => F.pure(la.asInstanceOf[Either[L, B]]) + } + )) + def bracketCase[A, B]( acquire: EitherT[F, L, A] )(use: A => EitherT[F, L, B])(release: (A, ExitCase[Throwable]) => EitherT[F, L, Unit]): EitherT[F, L, B] = @@ -166,6 +178,18 @@ object Sync { def raiseError[A](e: Throwable): OptionT[F, A] = OptionT.catsDataMonadErrorForOptionT[F, Throwable].raiseError(e) + override def redeem[A, B](fa: OptionT[F, A])(recover: Throwable => B, f: A => B): OptionT[F, B] = + OptionT(F.redeem(fa.value)(e => Some(recover(e)), la => la.map(f))) + + override def redeemWith[A, B](fa: OptionT[F, A])(recover: Throwable => OptionT[F, B], bind: A => OptionT[F, B]): OptionT[F, B] = + OptionT(F.redeemWith(fa.value)( + e => recover(e).value, + la => la match { + case Some(a) => bind(a).value + case _ => F.pure(None) + } + )) + def bracketCase[A, B]( acquire: OptionT[F, A] )(use: A => OptionT[F, B])(release: (A, ExitCase[Throwable]) => OptionT[F, Unit]): OptionT[F, B] = From 52c7403fca0d66d5e969116fcc7696e48a3ed7af Mon Sep 17 00:00:00 2001 From: Alexandru Nedelcu Date: Sat, 25 Apr 2020 15:21:49 +0300 Subject: [PATCH 2/2] Remove redeem overrides from Sync --- .../src/main/scala/cats/effect/Sync.scala | 24 ------------------- 1 file changed, 24 deletions(-) diff --git a/core/shared/src/main/scala/cats/effect/Sync.scala b/core/shared/src/main/scala/cats/effect/Sync.scala index d098e904ea..b865d3e834 100644 --- a/core/shared/src/main/scala/cats/effect/Sync.scala +++ b/core/shared/src/main/scala/cats/effect/Sync.scala @@ -118,18 +118,6 @@ object Sync { def raiseError[A](e: Throwable): EitherT[F, L, A] = EitherT.liftF(F.raiseError(e)) - override def redeem[A, B](fa: EitherT[F, L, A])(recover: Throwable => B, f: A => B): EitherT[F, L, B] = - EitherT(F.redeem(fa.value)(e => Right(recover(e)), la => la.map(f))) - - override def redeemWith[A, B](fa: EitherT[F, L, A])(recover: Throwable => EitherT[F, L, B], bind: A => EitherT[F, L, B]): EitherT[F, L, B] = - EitherT(F.redeemWith(fa.value)( - e => recover(e).value, - la => la match { - case Right(a) => bind(a).value - case _ => F.pure(la.asInstanceOf[Either[L, B]]) - } - )) - def bracketCase[A, B]( acquire: EitherT[F, L, A] )(use: A => EitherT[F, L, B])(release: (A, ExitCase[Throwable]) => EitherT[F, L, Unit]): EitherT[F, L, B] = @@ -178,18 +166,6 @@ object Sync { def raiseError[A](e: Throwable): OptionT[F, A] = OptionT.catsDataMonadErrorForOptionT[F, Throwable].raiseError(e) - override def redeem[A, B](fa: OptionT[F, A])(recover: Throwable => B, f: A => B): OptionT[F, B] = - OptionT(F.redeem(fa.value)(e => Some(recover(e)), la => la.map(f))) - - override def redeemWith[A, B](fa: OptionT[F, A])(recover: Throwable => OptionT[F, B], bind: A => OptionT[F, B]): OptionT[F, B] = - OptionT(F.redeemWith(fa.value)( - e => recover(e).value, - la => la match { - case Some(a) => bind(a).value - case _ => F.pure(None) - } - )) - def bracketCase[A, B]( acquire: OptionT[F, A] )(use: A => OptionT[F, B])(release: (A, ExitCase[Throwable]) => OptionT[F, Unit]): OptionT[F, B] =