Skip to content

Instantly share code, notes, and snippets.

@JoolsF
Last active June 2, 2022 21:19
Show Gist options
  • Save JoolsF/dc1d2a046fb21065710a94469cd7188a to your computer and use it in GitHub Desktop.
Save JoolsF/dc1d2a046fb21065710a94469cd7188a to your computer and use it in GitHub Desktop.
Kleisli Example 1
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