From d00417300344a5bf0f29f499fe4fbdffb21f7ad2 Mon Sep 17 00:00:00 2001 From: danicheg Date: Sat, 27 Jul 2024 13:57:57 +0400 Subject: [PATCH 1/3] Add FEitherOps#leftMapOrKeepIn --- shared/src/main/scala/mouse/feither.scala | 6 ++++++ shared/src/test/scala/mouse/FEitherSyntaxTest.scala | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/shared/src/main/scala/mouse/feither.scala b/shared/src/main/scala/mouse/feither.scala index b718fad..0ddc937 100644 --- a/shared/src/main/scala/mouse/feither.scala +++ b/shared/src/main/scala/mouse/feither.scala @@ -93,6 +93,12 @@ final class FEitherOps[F[_], L, R](private val felr: F[Either[L, R]]) extends An case r @ Right(_) => r.asInstanceOf[Right[A, R]] } + def leftMapOrKeepIn[LL >: L](pf: PartialFunction[L, LL])(implicit F: Functor[F]): F[Either[LL, R]] = + F.map(felr) { + case Left(a) => Left(pf.applyOrElse(a, identity[LL])) + case r: Right[L, R] => r.asInstanceOf[Right[LL, R]] + } + def leftAsIn[B](b: => B)(implicit F: Functor[F]): F[Either[B, R]] = leftMapIn(_ => b) diff --git a/shared/src/test/scala/mouse/FEitherSyntaxTest.scala b/shared/src/test/scala/mouse/FEitherSyntaxTest.scala index ea79008..fcd1888 100644 --- a/shared/src/test/scala/mouse/FEitherSyntaxTest.scala +++ b/shared/src/test/scala/mouse/FEitherSyntaxTest.scala @@ -114,6 +114,12 @@ class FEitherSyntaxTest extends MouseSuite { assertEquals(leftValue.leftMapIn(_ => ""), List("".asLeft[Int])) } + test("FEitherSyntax.leftMapOrKeepIn") { + assertEquals(rightValue.leftMapOrKeepIn { case "42" => "" }, rightValue) + assertEquals(leftValue.leftMapOrKeepIn { case "4242" => "" }, leftValue) + assertEquals(leftValue.leftMapOrKeepIn { case "42" => "" }, List(Left(""))) + } + test("FEitherSyntax.leftAsIn") { assertEquals(rightValue.leftAsIn(""), rightValue) assertEquals(leftValue.leftAsIn(""), List("".asLeft[Int])) From a7240cfbf1f96c674ba57dd66ea132cce0a4083e Mon Sep 17 00:00:00 2001 From: danicheg Date: Sat, 27 Jul 2024 14:06:36 +0400 Subject: [PATCH 2/3] Add FEitherOps#leftFlatMapOrKeepIn --- shared/src/main/scala/mouse/feither.scala | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/shared/src/main/scala/mouse/feither.scala b/shared/src/main/scala/mouse/feither.scala index 0ddc937..d8bd151 100644 --- a/shared/src/main/scala/mouse/feither.scala +++ b/shared/src/main/scala/mouse/feither.scala @@ -81,6 +81,14 @@ final class FEitherOps[F[_], L, R](private val felr: F[Either[L, R]]) extends An case r @ Right(_) => r.asInstanceOf[Right[A, B]] } + def leftFlatMapOrKeepIn[LL >: L, RR >: R]( + pf: PartialFunction[L, Either[LL, RR]] + )(implicit F: Functor[F]): F[Either[LL, RR]] = + F.map(felr) { + case l @ Left(a) => pf.applyOrElse(a, (_: L) => l) + case r: Right[L, R] => r.asInstanceOf[Right[LL, RR]] + } + def leftFlatMapF[A, B >: R](f: L => F[Either[A, B]])(implicit F: Monad[F]): F[Either[A, B]] = F.flatMap(felr) { case Left(left) => f(left) From 9d7b41abe4777d6364323e7b4fb8d084bd832701 Mon Sep 17 00:00:00 2001 From: danicheg Date: Sat, 27 Jul 2024 14:06:59 +0400 Subject: [PATCH 3/3] Add tests for FEitherOps#leftFlatMapOrKeepIn --- shared/src/test/scala/mouse/FEitherSyntaxTest.scala | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/shared/src/test/scala/mouse/FEitherSyntaxTest.scala b/shared/src/test/scala/mouse/FEitherSyntaxTest.scala index fcd1888..c1c5f0b 100644 --- a/shared/src/test/scala/mouse/FEitherSyntaxTest.scala +++ b/shared/src/test/scala/mouse/FEitherSyntaxTest.scala @@ -104,6 +104,13 @@ class FEitherSyntaxTest extends MouseSuite { assertEquals(leftValue.leftFlatMapIn(_ => "".asLeft[Int]), List("".asLeft[Int])) } + test("FEitherSyntax.leftFlatMapOrKeepIn") { + assertEquals(rightValue.leftFlatMapOrKeepIn { case "42" => Left("") }, rightValue) + assertEquals(leftValue.leftFlatMapOrKeepIn { case "4242" => Right(42) }, leftValue) + assertEquals(leftValue.leftFlatMapOrKeepIn { case "42" => Left("") }, List(Left(""))) + assertEquals(leftValue.leftFlatMapOrKeepIn { case "42" => Right(42) }, rightValue) + } + test("FEitherSyntax.leftFlatMapF") { assertEquals(rightValue.leftFlatMapF(_ => List("".asLeft[Int])), rightValue) assertEquals(leftValue.leftFlatMapF(_ => List("".asLeft[Int])), List("".asLeft[Int]))