# 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)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://george-jen.gitbook.io/data-science-and-apache-spark/anonymous-function.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
