Last active
June 2, 2022 21:19
-
-
Save JoolsF/dc1d2a046fb21065710a94469cd7188a to your computer and use it in GitHub Desktop.
Kleisli Example 1
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
import cats.data.Kleisli | |
import cats.implicits._ | |
import scala.util.Try | |
case class Foo(i: Int) | |
case class Bar(f: Foo) | |
val makeFoo: String => Option[Foo] = (s: String) => Try(s.toInt).toOption.map(Foo) | |
val makeBar: Foo => Option[Bar] = (f: Foo) => if (f.i < 0) None else Some(Bar(f)) | |
val makeFooKleisli: Kleisli[Option, String, Foo] = Kleisli(makeFoo) | |
val makeBarKleisli: Kleisli[Option, Foo, Bar] = Kleisli(makeBar) | |
val composed = makeFooKleisli andThen makeBarKleisli | |
composed.run("x") | |
composed.run("1") | |
composed.run("-1") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment