Integrating Common Higher-Order Functions with the For-Expressions in Scala
When you follow the functional programming paradigm, you probably need to create and handle loops. You can leverage recursion, but there is also another possible way: for-expressions. A for-expression represents a loop over a set of items, where you can make assignments, and apply filters. In for-expressions, you need to use the yield key word at the end, to let the for-expression returns values. Suppose you start from a List of five strings, and you want to capitalize them all into a new List. You can do it by using a for-expression, and you need to yield the new five capitalized strings.
For-expressions are also good candidates when it comes to the need to implement map and filter higher-order functions in a very synthetic and compact way.
In this lab, you will understand the components of for-expressions, and you will start working with them.
Learning Objectives
Upon completion of this beginner level lab, you will be able to:
- Understanding for-expressions
- Solving different problems using for-expressions
Intended Audience
This lab is intended for:
- Software engineers focused on the functional paradigm that needs to solve loops problems in a compact way
- Data engineers that need to handle and apply operations on data sets
Prerequisites
To get the most out of this lab, you should have basic knowledge of Scala. To achieve this, we suggest taking the following labs:
- Understanding Basic Scala Elements for Functional Programming
- Starting using Recursion with Scala
- Creating and Handling Classes and Hierarchies in Scala
- Introducing Generics for Classes and Functions in Scala
- Leveraging Common Scala Immutable Data Structures
- Writing and Using Higher-Order and Anonymous Functions in Scala
Updates
April 20th, 2023 - Updated theia to enable autosave
November 18th, 2022 - Updated the instructions and screenshots to reflect the latest UI
Stefano studies Computer Science and is passionate about technology. He loves working with Cloud services and learning all the best practices for them. Google Cloud Platform and Amazon Web Services are the cloud providers he prefers. He is a Google Cloud Certified Associate Cloud Engineer. Node.js is the programming language he always uses to code. When he's not involved in studying or working, Stefano loves riding his motorbike and exploring new places.