100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Swift

Higher-Order Functions in Swift

Explore functions that take other functions or closures as arguments, such as map, filter, and reduce.

Functions & ClosuresIntermediate9 min readJul 8, 2026
Analogies

Introduction

A higher-order function is a function that either takes one or more functions or closures as parameters, returns a function, or both. Swift's standard library ships with several higher-order functions on collections that make it possible to write concise, expressive, and idiomatic code without manually writing loops. The most common examples are map, filter, reduce, sorted(by:), compactMap, and forEach.

🏏

Cricket analogy: A team analyst's function that accepts "apply this filtering strategy" as an input, like passing in a spin-friendly-pitch strategy closure, is a higher-order function, much like map, filter, and reduce accept closures to process ball-by-ball data.

Syntax

swift
array.map { transform in ... }
array.filter { predicate in ... }
array.reduce(initialResult) { partial, element in ... }
array.sorted(by: { a, b in ... })
array.compactMap { transform in ... }
array.forEach { element in ... }

Explanation

map transforms each element of a collection and returns a new collection of the transformed values. filter returns a new collection containing only the elements that satisfy a given condition (predicate). reduce combines all elements into a single value using an accumulator closure. compactMap behaves like map but also strips out nil results, unwrapping optionals in the process. forEach runs a closure on every element purely for side effects, without producing a new collection. sorted(by:) returns a new sorted array based on a comparison closure. Because these functions accept closures as arguments, they qualify as higher-order functions, and using trailing closure syntax with them is idiomatic Swift.

🏏

Cricket analogy: map converts each ball's raw speed into km/h across a list of deliveries; filter keeps only sixes from an over; reduce totals a batsman's runs across an innings using an accumulator, all without writing a manual loop.

Example

swift
let numbers = [1, 2, 3, 4, 5, 6]

let doubled = numbers.map { $0 * 2 }
let evens = numbers.filter { $0 % 2 == 0 }
let sum = numbers.reduce(0) { $0 + $1 }
let strings = ["1", "two", "3"]
let parsed = strings.compactMap { Int($0) }

print(doubled)
print(evens)
print(sum)
print(parsed)

Output

swift
[2, 4, 6, 8, 10, 12]
[2, 4, 6]
21
[1, 3]

Key Takeaways

  • Higher-order functions take or return other functions or closures.
  • map transforms elements; filter keeps elements matching a predicate.
  • reduce combines all elements into a single accumulated value.
  • compactMap maps and removes nil results, unwrapping optionals.
  • forEach performs a side-effecting action on each element without returning a new collection.
  • These functions are heavily used in idiomatic Swift instead of manual for-loops.

Practice what you learned

Was this page helpful?

Topics covered

#Swift#SwiftProgrammingStudyNotes#Programming#HigherOrderFunctionsInSwift#Higher#Order#Functions#Syntax#StudyNotes#SkillVeris