20 Higher order functions

 

After reading this lesson, you will be able to

  • Define functions that have functions as parameters
  • Implement functions that return functions
  • Build powerful abstractions that remove code duplication

Functions are first-class citizens in Scala: you can use them as parameters or return them as the result of some computation. Higher order functions are functions that accept other functions as parameters, return functions, or both. Their use allows you to create powerful abstractions that reduce duplication and increase the reusability of your code. In the capstone, you’ll use higher order functions to extract information on the winner of the game “Paper, Rock, Scissors, Lizard, Spock!”

20.1 Functions as parameters

Suppose you want to perform some simple statistics on a string to calculate its length and how many letters or digits it has.

20.2 Functions as return values

Summary

Answers to quick checks