Skip to content

Instantly share code, notes, and snippets.

@kaushik88
Last active October 5, 2016 04:38
Show Gist options
  • Save kaushik88/00f3812f4b56751ad979ca1da99af738 to your computer and use it in GitHub Desktop.
Save kaushik88/00f3812f4b56751ad979ca1da99af738 to your computer and use it in GitHub Desktop.
Map-FlatMap-Filter
val numbers : List[Int] = List(1,2,3,4,5,6,7,8,9,10)
def filter(p: Int => Boolean): List[Int]
val oddNumbers : List[Int] = numbers.filter{case number :Int => number % 2 == 1} // List(1, 3, 5, 7, 9)
def flatMap[B](f: Int => scala.collection.GenTraversableOnce[B]): scala.collection.TraversableOnce[B]
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)
def map[B](f: Int => B): scala.collection.TraversableOnce[B]
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