Higher-Order Functions
In this lesson, you will learn about Higher-Order Functions in Scala. Higher-order functions are one of the most important concepts in functional programming and are used extensively in real-world Scala applications.
They allow you to write flexible, reusable, and expressive code by treating functions as values.
What Is a Higher-Order Function?
A higher-order function is a function that does at least one of the following:
- Takes one or more functions as parameters
- Returns a function as a result
Scala supports higher-order functions natively, making functional programming natural and powerful.
Passing Functions as Parameters
You can pass a function as an argument to another function. This allows behavior to be customized without rewriting code.
def operate(a: Int, b: Int, f: (Int, Int) => Int): Int = {
f(a, b)
}
val sum = operate(5, 3, (x, y) => x + y)
val product = operate(5, 3, (x, y) => x * y)
println(sum)
println(product)
Here, the function operate receives another function f as a parameter.
Using Named Functions
Instead of anonymous functions, you can pass named functions.
def add(a: Int, b: Int): Int = a + b
def multiply(a: Int, b: Int): Int = a * b
val r1 = operate(4, 2, add)
val r2 = operate(4, 2, multiply)
println(r1)
println(r2)
This improves readability and reuse in larger applications.
Returning Functions from Functions
Higher-order functions can also return functions. This is useful for creating configurable logic.
def multiplier(factor: Int): Int => Int = {
(x: Int) => x * factor
}
val double = multiplier(2)
val triple = multiplier(3)
println(double(5))
println(triple(5))
The function multiplier returns another function based on the input factor.
Higher-Order Functions with Collections
Scala collections heavily rely on higher-order functions.
Functions like map, filter, and foreach accept functions as arguments.
val numbers = List(1, 2, 3, 4, 5)
numbers.foreach(n => println(n))
val squared = numbers.map(n => n * n)
val evenNumbers = numbers.filter(n => n % 2 == 0)
println(squared)
println(evenNumbers)
These operations avoid loops and make code concise and expressive.
Why Higher-Order Functions Matter
- Reduce code duplication
- Improve code readability
- Enable flexible and reusable logic
- Encourage functional programming style
Real-World Use Cases
Higher-order functions are commonly used for:
- Data transformations
- Validation rules
- Custom sorting and filtering
- Event handling
📝 Practice Exercises
Exercise 1
Write a higher-order function that applies a function to two numbers.
Exercise 2
Create a function that returns another function to add a fixed number.
Exercise 3
Use filter to extract numbers greater than 10 from a list.
✅ Practice Answers
Answer 1
def applyFn(a: Int, b: Int, fn: (Int, Int) => Int): Int = {
fn(a, b)
}
Answer 2
def adder(x: Int): Int => Int = {
(y: Int) => x + y
}
Answer 3
val nums = List(5, 12, 18, 7)
val result = nums.filter(_ > 10)
result
What’s Next?
In the next lesson, you will learn about Lambdas and Closures, which build directly on higher-order functions.