Created
May 5, 2012 19:40
-
-
Save rares/2605071 to your computer and use it in GitHub Desktop.
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
class Box[T](val value: T) { | |
def map[B](f: T => B) = new Box(f(this.value)) | |
def flatMap[B](f: T => Box[B]) = f(this.value) | |
def apply[B](f: Box[T => B]) = new Box(f.value(this.value)) | |
} | |
println(new Box("cool").map((a:String) => a.toUpperCase).value) | |
println(new Box("cool").flatMap((a:String) => new Box(a.toUpperCase)).value) | |
println(new Box("cool").apply(new Box((a:String) => a.toUpperCase)).value) | |
println(new Box("cool") | |
.map((a:String) => a.toUpperCase) | |
.map((a:String) => a + " story!") | |
.value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment