hands-on lab

Writing and Using Higher-Order and Anonymous Functions in Scala

Difficulty: Intermediate
Duration: Up to 45 minutes
Students: 71
Rating: 5/5
Get guided in a real environmentPractice with a step-by-step scenario in a real, provisioned environment.
Learn and validateUse validations to check your solutions every step of the way.
See resultsTrack your knowledge and monitor your progress.

Description

When you use the functional programming paradigm, the function is the core component you need to focus on and use. It can be used as a value, so you can set a value for it (defining the body of a function), and you can use it as a function's argument, and also as a return value. A function that takes a function as an argument or returns it as a value is called a higher-order function.

Most of the time, the functions you need to pass as an argument to another function are not shared and used by other components. In this situation, creating the functions could pollute the environment namespace (you have a lot of functions that are only used by a single entity or a single time). To avoid this scenario, you can pass the body of a function to another function without defining the function you need to pass as an argument; you simply pass the value. This kind of function is called anonymous function (you could hear about it also as a lambda function). That's because it doesn't have a formal definition, and can't be referred to.

In this lab, you will start using higher-order functions and anonymous, with a focus on the three most used higher-order functions in functional programming: map, filter, and reduce.

Learning Objectives

Upon completion of this beginner level lab, you will be able to:

  • Understand higher-order functions and anonymous functions
  • Start using higher-order functions and anonymous functions to better solve problems

Intended Audience

This lab is intended for:

  • Software engineers focusing on the functional programming paradigm
  • Data engineers need to follow a clear way to handle and manipulate data

Prerequisites

To get the most out of this lab, you should have basic knowledge of Scala. To achieve this, the following labs are suggested:

Updates

April 20th, 2023 - Updated theia to enable autosave

Covered topics

Lab steps

Connecting to the Scala Web IDE
Working with Higher-Order and Anonymous Functions