Skip to content

Instantly share code, notes, and snippets.

@sshark
Created August 28, 2021 13:16
Show Gist options
  • Select an option

  • Save sshark/2f1899b96574acd673ce2b69464645b2 to your computer and use it in GitHub Desktop.

Select an option

Save sshark/2f1899b96574acd673ce2b69464645b2 to your computer and use it in GitHub Desktop.
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