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.
1
//create a RDD object
2
val rdd=sc.parallelize(List(1,2,3,4,5))
3
//rdd: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[0] at parallelize at <console>:29
4
//add 1 to each element (row) of RDD
5
rdd.map(x=>x+1).collect
6
//x=>x+1 is a anonymous function, like lambda in Pyton
7
//return only even elements
8
rdd.filter(x=>x%2==0).collect
9
//res26: Array[Int] = Array(2, 4)
Copied!
​
Last modified 1yr ago
Copy link