Skip to content

Instantly share code, notes, and snippets.

@monkeygroover
Last active March 10, 2016 14:27
Show Gist options
  • Save monkeygroover/fe12b37d63cf5c33f37a to your computer and use it in GitHub Desktop.
Save monkeygroover/fe12b37d63cf5c33f37a to your computer and use it in GitHub Desktop.
Kleisli compositions
val foo = (n: Int) => n.toString
val bar = (s: String) => s.toInt
// composing
val f0 = bar compose foo
println(f0(3))
//what if the functions could fail..
val oFoo: (Int) => Option[String] = foo map {Some(_)}
val oBar: (String) => Option[Int] = bar map {Some(_)}
//val ddd = oFoo andThen oBar //no longer works..
// raw map style
val f1 = oFoo(_: Int) flatMap { s => oBar(s)}
// symbolic bind style
val f2 = oFoo(_: Int) >>= oBar
// kleisli magic in a point-free style
val f3 = kleisli(oFoo) >==> oBar
println(f1(3))
println(f2(3))
println(f3(3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment