Last active
October 5, 2016 04:38
-
-
Save kaushik88/00f3812f4b56751ad979ca1da99af738 to your computer and use it in GitHub Desktop.
Map-FlatMap-Filter
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 numbers : List[Int] = List(1,2,3,4,5,6,7,8,9,10) |
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
def filter(p: Int => Boolean): List[Int] |
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 oddNumbers : List[Int] = numbers.filter{case number :Int => number % 2 == 1} // List(1, 3, 5, 7, 9) |
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
def flatMap[B](f: Int => scala.collection.GenTraversableOnce[B]): scala.collection.TraversableOnce[B] |
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 squaresAndCubes :List[Int] = numbers.flatMap{case number :Int => List(number * number, number * number * number)} | |
// List(1, 1, 4, 8, 9, 27, 16, 64, 25, 125, 36, 216, 49, 343, 64, 512, 81, 729, 100, 1000) |
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
def map[B](f: Int => B): scala.collection.TraversableOnce[B] |
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 squareNumbers : List[Int] = numbers.map{case number :Int => number * number} | |
// List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment