Skip to content

Instantly share code, notes, and snippets.

Created June 23, 2013 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/5846380 to your computer and use it in GitHub Desktop.
Save anonymous/5846380 to your computer and use it in GitHub Desktop.
Inline vs Chaining Example
object Chaining {
val xs = List(1,2,3,4,5) //> xs : List[Int] = List(1, 2, 3, 4, 5)
def square(x:Int):Int = x*x //> square: (x: Int)Int
def even(x:Int):Boolean = x%2 == 0 //> even: (x: Int)Boolean
//// SHORT EXAMPLE
xs map square // <-- Prefer //> res0: List[Int] = List(1, 4, 9, 16, 25)
// vs
xs.map(square) //> res1: List[Int] = List(1, 4, 9, 16, 25)
//// LONG EXAMPLE
xs filter even map square reduce (_+_) //> res2: Int = 20
// vs
xs.filter(even) // <-- Prefer
.map(square)
.reduce(_+_) //> res3: Int = 20
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment