Slide 21
Slide 21 text
ͦΕͧΕͷ.POBE&SSPSͷ࣮Λࠩ͠ࠐΉ͚ͩ
import cats.instances.try_._
val resTrySuccess = program[Try, Throwable](Success(43), new RuntimeException(“Error"))
// = Success(43)
val resTryFailure = program[Try, Throwable](Success(-43), new RuntimeException("Error"))
// = Failure(RuntimeException("Error"))
import cats.instances.future._
val resFutureSuccess = program[Future, Throwable](Future(43), new RuntimeException(“Error"))
// = onComplete => Success(43)
val resFutureFailure = program[Future, Throwable](Future(-43), new RuntimeException(“Error"))
// = onComplete => Failure(RuntimeException("Error"))
'VUVSF
&JUIFS
5SZ
import cats.syntax.either._
import cats.instances.either._
val resEitherSuccess = program[({type E[A] = Either[String, A]})#E, String](43.asRight[String], "Error")
// Right(86)
val resEitherFailure = program[({type E[A] = Either[String, A]})#E, String](-43.asRight[String], "Error")
// Left("Error")
def program[F[_], E](value: F[Int], e: => E)(implicit ME: MonadError[F, E]): F[Int] =
ME.ensure(value)(e)(_ >= 0).map(_*2)
.POBE&SSPS*OTUBODFJODBUT