From b18959979d8753cba67ad1d827a35212c977c162 Mon Sep 17 00:00:00 2001 From: GreyPlane <709327148@qq.com> Date: Fri, 19 May 2023 16:49:43 +0800 Subject: [PATCH 1/4] feat: +EitherT.liftRedeemK --- core/src/main/scala/cats/data/EitherT.scala | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/core/src/main/scala/cats/data/EitherT.scala b/core/src/main/scala/cats/data/EitherT.scala index 3ee00822d7..ed46facd05 100644 --- a/core/src/main/scala/cats/data/EitherT.scala +++ b/core/src/main/scala/cats/data/EitherT.scala @@ -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, Unit, Int] = EitherT.liftRedeemK[Option, Unit, String](f).apply(a) + * scala> b.value + * res0: Option[Either[Unit, Int]] = Some(Left("panic!")) + * + * scala> val a2: Option[Int] = Some(42) + * scala> val b2: EitherT[Option, Unit, Int] = EitherT.liftRedeemK[Option, Unit, String](f).apply(a2) + * scala> b2.value + * res1: Option[Either[Unit, 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) From 8902a5106450a1acb6e1295e3e1f19f2143ddc83 Mon Sep 17 00:00:00 2001 From: GreyPlane <709327148@qq.com> Date: Fri, 19 May 2023 17:15:17 +0800 Subject: [PATCH 2/4] fix: fix scaladoc --- core/src/main/scala/cats/data/EitherT.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/main/scala/cats/data/EitherT.scala b/core/src/main/scala/cats/data/EitherT.scala index ed46facd05..7668575dd6 100644 --- a/core/src/main/scala/cats/data/EitherT.scala +++ b/core/src/main/scala/cats/data/EitherT.scala @@ -849,14 +849,14 @@ object EitherT extends EitherTInstances { * scala> import cats._, data._, implicits._ * scala> val f: Unit => String = Function.const("panic!") * scala> val a: Option[Int] = None - * scala> val b: EitherT[Option, Unit, Int] = EitherT.liftRedeemK[Option, Unit, String](f).apply(a) + * scala> val b: EitherT[Option, String, Int] = EitherT.liftRedeemK[Option, Unit, String](f).apply(a) * scala> b.value - * res0: Option[Either[Unit, Int]] = Some(Left("panic!")) + * res0: Option[Either[String, Int]] = Some(Left("panic!")) * * scala> val a2: Option[Int] = Some(42) - * scala> val b2: EitherT[Option, Unit, Int] = EitherT.liftRedeemK[Option, Unit, String](f).apply(a2) + * scala> val b2: EitherT[Option, String, Int] = EitherT.liftRedeemK[Option, Unit, String](f).apply(a2) * scala> b2.value - * res1: Option[Either[Unit, Int]] = Some(Right(42)) + * 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, *] = From 1db5502bcd667cc3f5cc49e5eee7be2f86a44e9a Mon Sep 17 00:00:00 2001 From: GreyPlane <709327148@qq.com> Date: Sat, 20 May 2023 14:00:42 +0800 Subject: [PATCH 3/4] fix: fix scaladoc --- core/src/main/scala/cats/data/EitherT.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/scala/cats/data/EitherT.scala b/core/src/main/scala/cats/data/EitherT.scala index 7668575dd6..5da0494e62 100644 --- a/core/src/main/scala/cats/data/EitherT.scala +++ b/core/src/main/scala/cats/data/EitherT.scala @@ -851,7 +851,7 @@ object EitherT extends EitherTInstances { * 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!")) + * 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) From 9b99ea54a96dcb1c1927ad987cd8660eb1d75edb Mon Sep 17 00:00:00 2001 From: GreyPlane <709327148@qq.com> Date: Tue, 23 May 2023 11:41:04 +0800 Subject: [PATCH 4/4] chore: rename function --- core/src/main/scala/cats/data/EitherT.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/main/scala/cats/data/EitherT.scala b/core/src/main/scala/cats/data/EitherT.scala index 5da0494e62..076f7ddfd7 100644 --- a/core/src/main/scala/cats/data/EitherT.scala +++ b/core/src/main/scala/cats/data/EitherT.scala @@ -849,17 +849,17 @@ object EitherT extends EitherTInstances { * 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> val b: EitherT[Option, String, Int] = EitherT.liftAdaptErrorK[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> val b2: EitherT[Option, String, Int] = EitherT.liftAdaptErrorK[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, *] = + final def liftAdaptErrorK[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)) }