Created
August 28, 2021 13:16
-
-
Save sshark/2f1899b96574acd673ce2b69464645b2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import cats.{Applicative, ApplicativeError} | |
| import cats.implicits._ | |
| object ApplicationErrorExampleApp extends App { | |
| val f: List[Int] => Int = (xs: List[Int]) => xs.sum | |
| val g: List[Int] => Int = (xs: List[Int]) => xs.size | |
| val xs = List(List(1, 2, 3)) | |
| Applicative[List].<*>(List(f))(xs).foreach(println) | |
| List(f) <*> xs | |
| // Using kind-projector plugin to replace the hideous type lambda syntax, | |
| // ({type L[F[_]] = ApplicativeError[F, String]})#L, with a readable syntax | |
| def divide[F[_]: ApplicativeError[*[_], String]](a: Int, b: Int): F[Int] = if (b == 0) | |
| ApplicativeError[F, String].raiseError("Cannot be divided by zero") | |
| else ApplicativeError[F, String].pure(a / b) | |
| println(divide(100, 10)) | |
| println(divide(100, 0)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment