Skip to content

Commit

Permalink
Add FEitherOps#leftFlatMapOrKeepIn
Browse files Browse the repository at this point in the history
  • Loading branch information
danicheg committed Jul 27, 2024
1 parent d004173 commit a7240cf
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions shared/src/main/scala/mouse/feither.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a7240cf

Please sign in to comment.