higher-order-functions-in-scala
admin
Scala Higher-Order Function Example
Updated: February 2025 | By Shubham Mishra
A higher order function is a function that:
This concept is not unique to Scala—it’s also found in languages like Java 8+ (e.g., lambda expressions). However, Scala’s functional programming capabilities make it particularly well-suited for higher order functions.The terminology can get a bit confusing at this point, and we use the phrase “higher order function” for both methods and functions that take functions as parameters or that return a function.
Let’s start with a simple example of a higher order function in Scala. The map
function is one of the most common higher order functions available for collections.
val mySalaries = Seq(70000, 20000, 40000)
val doubleSalary = (x: Int) => x * 2
val newSalaries = mySalaries.map(doubleSalary) // List(140000, 40000, 80000)
In this example:
map
is a higher order function.doubleSalary
is a function passed as an argument to map
.Let’s create a more advanced example to demonstrate the power of higher order functions. Suppose we want to apply different types of salary promotions:
object SalaryPromotionMaker {
def smallPromotion(salaries: List[Double]): List[Double] = salaries.map(salary => salary * 1.1)
def greatPromotion(salaries: List[Double]): List[Double] = salaries.map(salary => salary * math.log(salary))
def hugePromotion(salaries: List[Double]): List[Double] = salaries.map(salary => salary * salary)
}
Notice how each method varies only by the multiplication factor. We can simplify this by extracting the repeated code into a higher order function:
object SalaryPromotionMaker {
private def promotion(salaries: List[Double], promotionFunction: Double => Double): List[Double] =
salaries.map(promotionFunction)
def smallPromotion(salaries: List[Double]): List[Double] = promotion(salaries, salary => salary * 1.1)
def greatPromotion(salaries: List[Double]): List[Double] = promotion(salaries, salary => salary * math.log(salary))
def hugePromotion(salaries: List[Double]): List[Double] = promotion(salaries, salary => salary * salary)
}
Here, promotion
is a higher order function that takes a function (promotionFunction
) as a parameter.
You can pass functions as arguments to other functions. This is useful when you want to provide different behaviors dynamically.
// Higher-order function that takes a function f and two integers as arguments
def operate(f: (Int, Int) => Int, x: Int, y: Int): Int = {
f(x, y)
}
// Define functions to pass as arguments
def add(a: Int, b: Int): Int = a + b
def multiply(a: Int, b: Int): Int = a * b
// Call the higher-order function with different functions
val resultAdd = operate(add, 3, 5) // Result: 8 (3 + 5)
val resultMultiply = operate(multiply, 3, 5) // Result: 15 (3 * 5)
println(resultAdd)
println(resultMultiply)
Higher order functions offer several benefits:
map
, filter
, and reduce
.A higher order function is a function that takes one or more functions as parameters or returns a function as a result.
Examples include map
, filter
, reduce
, and custom functions like promotion
in the salary example above.
They promote code reuse, abstraction, and flexibility, making your code more modular and easier to maintain.
Yes, higher order functions can return functions as results.
Higher order functions are a cornerstone of functional programming in Scala. They allow you to write reusable, abstract, and flexible code, making your programs more modular and easier to maintain. By mastering higher order functions, you can unlock the full potential of Scala’s functional programming capabilities.
Start experimenting with higher order functions today! Try creating your own examples, explore Scala’s standard library, and build more expressive and efficient code.