Created
June 23, 2013 20:15
-
-
Save anonymous/5846380 to your computer and use it in GitHub Desktop.
Inline vs Chaining Example
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
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