Anonymous function

Like lambda in Python, Scala has similar anonymous function, with different syntax. They are typically used in map and filter and other methods.

//create a RDD object
val rdd=sc.parallelize(List(1,2,3,4,5))
//rdd: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[0] at parallelize at <console>:29
//add 1 to each element (row) of RDD
rdd.map(x=>x+1).collect
//x=>x+1 is a anonymous function, like lambda in Pyton
//return only even elements 
rdd.filter(x=>x%2==0).collect
//res26: Array[Int] = Array(2, 4)

Last updated