Last active
March 10, 2016 14:27
-
-
Save monkeygroover/fe12b37d63cf5c33f37a to your computer and use it in GitHub Desktop.
Kleisli compositions
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
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