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

Add recover either #506

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 11 additions & 3 deletions shared/src/main/scala/mouse/anyf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ package mouse

import cats.data.EitherT
import cats.data.OptionT
import cats.~>
import cats.Functor
import cats.{ApplicativeError, Functor, MonadError, ~>}

trait AnyFSyntax {
implicit final def anyfSyntaxMouse[F[_], A](fa: F[A]): AnyFOps[F, A] = new AnyFOps(fa)
Expand All @@ -49,4 +48,13 @@ final class AnyFOps[F[_], A](private val fa: F[A]) extends AnyVal {
def liftOptionT(implicit F: Functor[F]): OptionT[F, A] =
OptionT.liftF(fa)

}
// applicativeError
def recoverAsLeft[E, L](pf: PartialFunction[E, L])(implicit F: ApplicativeError[F, E]): F[Either[L, A]] =
recoverEither(pf.andThen(Left(_)))

def recoverAsRight[E](pf: PartialFunction[E, A])(implicit F: ApplicativeError[F, E]): F[Either[Nothing, A]] =
recoverEither(pf.andThen(Right(_)))

def recoverEither[E, L](pf: PartialFunction[E, Either[L, A]])(implicit F: ApplicativeError[F, E]): F[Either[L, A]] =
F.recover(mapAsRight[L])(pf)
Comment on lines +58 to +59
Copy link
Member

Choose a reason for hiding this comment

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

When #508 is published, It can be used as a counterpart:

val fa: F[Unit] = ???

fa.attempt.leftFlatMapOrKeepIn(pf)

But if we take allocations into consideration, it's probably better to have a distinct syntax for that.

}
57 changes: 54 additions & 3 deletions shared/src/test/scala/mouse/AnyFSyntaxTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ package mouse

import cats.data.EitherT
import cats.data.OptionT
import cats.syntax.option._
import cats.syntax.either._
import cats.{~>, Id}
import cats.syntax.option.*
import cats.syntax.either.*
import cats.{Id, ~>}

import scala.util.{Success, Try}

class AnyFSyntaxTest extends MouseSuite {
private val emptyK = new (List ~> List) {
Expand Down Expand Up @@ -75,6 +77,55 @@ class AnyFSyntaxTest extends MouseSuite {
assertEquals(List(1).mapAsSome, List(1.some))
}

test("AnyFSyntax.recoverEither") {
assertEquals(
Try[Int](1).recoverEither {
case _: Throwable => Right(2)
},
Success(1.asRight)
)

assertEquals(
Try[Int](throw new RuntimeException("boom")).recoverEither {
case _: Throwable => Right(2)
},
Success(2.asRight)
)
}


test("AnyFSyntax.recoverAsRight") {
assertEquals(
Try[Int](1).recoverAsRight {
case _: Throwable => 2
},
Success(1.asRight)
)

assertEquals(
Try[Int](throw new RuntimeException("boom")).recoverAsRight {
case _: Throwable => 2
},
Success(2.asRight)
)
}

test("AnyFSyntax.recoverAsLeft") {
assertEquals(
Try(1).recoverAsLeft {
case _: Throwable => "foo"
},
Success(1.asRight)
)

assertEquals(
Try(throw new RuntimeException("boom")).recoverAsLeft {
case _: Throwable => "error"
},
Success("error".asLeft)
)
}

test("AnyFSyntax.liftEitherT") {
assertEquals(List(1).liftEitherT[String], EitherT(List(1.asRight[String])))
}
Expand Down
Loading