# Working with Higher-Order and Anonymous Functions

## Lab Steps

Connecting to the Scala Web IDE
Working with Higher-Order and Anonymous Functions
Need help? Contact our support team

### Introduction

Higher-order functions and anonymous functions are helpful components when it comes to the need to leverage the functional programming paradigm. They let you write more readable and compact code. In this lab step, you will start using anonymous functions and higher-order functions. You'll mainly focus on the most common and important higher-order functions for immutable data structures: map, filter, reduce.

### Instructions

1. Click on the Explorer icon, and open the Main.scala file:

2. Enter the following code snippet inside the main function to create a List and leverage the map higher-order function along with a function that calculates the square:

```content-copyCopy code```1
val result = List(1, 2, 3, 4).map(x => x * x)``````

The map higher-order function takes a function as an argument and returns the collection (in this case a List) whose items are the same in the original list with the function applied. You used an anonymous function as the argument passed to the map function (x => x*x). You didn't define any new function, but just passed the value of a function that calculates the square. To define an anonymous function, you need to follow this schema:

```content-copyCopy code```1
(x1, x2, ..., xn) => {expressions} // The {} brackets can be avoided when the expression is on a single statement``````

You define the arguments of the anonymous function between parenthesis; you define the body of the function between the braces.

3. Enter the following code snippet inside the main function to create a `List` and leverage the filter higher-order function along with a boolean anonymous function:

```content-copyCopy code```1
val filtered = List(1, 2, 3, 4, 5, 6).filter(x => x % 2 == 0)``````

The filter higher-order function takes a boolean function as an argument and returns the collection (in this case a List) that contains the items of the original list the boolean function returns true for.

4. Enter the following code snippet inside the main function to create a `List` and leverage the reduce higher-order function along with an anonymous function that multipliesÂ items:

```content-copyCopy code```1
val reduced = List(1, 2, 3, 4, 5, 6).reduce((x, y) => x * y)``````

The reduce higher-order function takes a two-argument functionÂ andÂ applies itÂ toÂ all ofÂ the items inÂ the collection from left to right. The resultÂ is notÂ a collection, but ratherÂ the given collection isÂ reduced to a single value. In theÂ code above each element is multiplied together resulting in the product of all the items.

5. Enter the following code snippet inside the main function to compose the three functions and generate a single final value:

```content-copyCopy code```1
val composed = List(1, 2, 4, 6, 7, 8, 10).filter(x => x % 2 == 0).map(y => y * 2).reduce((i, j) => i + j)``````

The filter function will filter only for the even values, the map will calculate the double for each value, the reduce then will make a final sum of the values.

6. Enter the following code snippet inside the main function to print out the values of the operations previously written:

```content-copyCopy code```1
2
3
4
println("result is: " + result)
println("filtered is: " + filtered)
println("reduced is: " + reduced)
println("composed is: " + composed)``````

The complete MapEx.scala content will look like this:

```content-copyCopy code```1
2
3
4
5
6
7
8
9
10
11
12
13
14
object Main {
def main(args: Array[String]) = {
val result = List(1, 2, 3, 4).map(x => x * x)
val filtered = List(1, 2, 3, 4, 5, 6).filter(x => x % 2 == 0)
val reduced = List(1, 2, 3, 4, 5, 6).reduce((x, y) => x * y)

val composed = List(1, 2, 4, 6, 7, 8, 10).filter(x => x % 2 == 0).map(y => y * 2).reduce((i, j) => i + j)

println("result is: " + result)
println("filtered is: " + filtered)
println("reduced is: " + reduced)
println("composed is: " + composed)
}
}``````
7. Save the content of the file by clicking on File -> Save All.

8. Expand the Terminal dropdown, and click on New Terminal. A terminal window will open:

9. Execute the following command:

```content-copyCopy code```1
scalac Main.scala``````
10. Execute the following command to launch the Scala source you just compiled:

```content-copyCopy code```1
scala Main``````

### Summary

In this lab step, you started using anonymous functions and higher-order functions.Â Formally defined functions can be used along with higher-order functions. However, anonymous functions are most common becauseÂ they don't pollute the namespace.

Validation checks
1Checks
Worked with Higher-Order and Anonymous Functions

Checked that the higher-order functions have been used along with the anonymous functions.

Scala