Skip to content

Instantly share code, notes, and snippets.

@rares
Created May 5, 2012 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rares/2605071 to your computer and use it in GitHub Desktop.
Save rares/2605071 to your computer and use it in GitHub Desktop.
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