forked from typelevel/cats
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance EitherT and fix typelevel#2161: add MonadError.redeem/redeemWith
- Loading branch information
Showing
18 changed files
with
1,275 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
core/src/main/scala-2.11-/cats/internals/FutureShims.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cats.internals | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
private[cats] object FutureShims { | ||
/** | ||
* Exposes the new `Future#transformWith` from Scala 2.12 in a way | ||
* that's compatible with Scala 2.11. | ||
*/ | ||
def redeemWith[A, B](fa: Future[A])(fe: Throwable => Future[B], fs: A => Future[B]) | ||
(implicit ec: ExecutionContext): Future[B] = | ||
attempt(fa).flatMap(_.fold(fe, fs)) | ||
|
||
/** | ||
* Exposes an optimized `attempt` for `Future` whose implementation | ||
* depends on the Scala version. | ||
*/ | ||
def attempt[A](fa: Future[A])(implicit ec: ExecutionContext): Future[Either[Throwable, A]] = | ||
fa.map(Right[Throwable, A]).recover { case e => Left(e) } | ||
} |
30 changes: 30 additions & 0 deletions
30
core/src/main/scala-2.12+/cats/internals/FutureShims.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cats.internals | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
import scala.util.{Failure, Success} | ||
|
||
private[cats] object FutureShims { | ||
/** | ||
* Exposes the new `Future#transformWith` from Scala 2.12 in a way | ||
* that's compatible with Scala 2.11. | ||
*/ | ||
def redeemWith[A, B](fa: Future[A])(fe: Throwable => Future[B], fs: A => Future[B]) | ||
(implicit ec: ExecutionContext): Future[B] = { | ||
|
||
fa.transformWith { | ||
case Success(a) => fs(a) | ||
case Failure(e) => fe(e) | ||
} | ||
} | ||
|
||
/** | ||
* Exposes an optimized `attempt` for `Future` whose implementation | ||
* depends on the Scala version. | ||
*/ | ||
def attempt[A](fa: Future[A])(implicit ec: ExecutionContext): Future[Either[Throwable, A]] = | ||
fa.transformWith(r => Future.successful( | ||
r match { | ||
case Success(a) => Right(a) | ||
case Failure(e) => Left(e) | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.