Working with Higher-Order and Anonymous Functions

Lab Steps

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

Here you can find the instructions for this specific Lab Step.

If you are ready for a real environment experience please start the Lab. Keep in mind that you'll need to start from the first step.

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:

    alt

  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:

    Copy 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:

    Copy 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:

    Copy 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:

    Copy 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:

    Copy 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:

    Copy 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:

    Copy 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:

    alt

  9. Execute the following command:

    Copy code
    1
    scalac Main.scala
  10. Execute the following command to launch the Scala source you just compiled:

    Copy code
    1
    scala Main

    alt

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