✅ List of Common Higher-Order Functions in Swift
map(_:)
filter(_:)
reduce(_:_: )
compactMap(_:)
flatMap(_:)
forEach(_:)
sorted(by:) / sort(by:)
contains(where:)
allSatisfy(_:)
first(where:)
drop(while:)
prefix(while:)
zip(_:_:) (used with closures)
removeAll(where:)
partition(by:)
1. map(_:) 3. reduce(_:_: )
📌 Purpose: 📌 Purpose:
Transforms each element of a collection Combines all elements of a collection into a
using a closure. single value (e.g., sum, product, string).
✅ Example: ✅ Example:
let numbers = [1, 2, 3, 4]
let numbers = [1, 2, 3, 4]
let squares = numbers.map { $0 * $0 }
let sum = numbers.reduce(0) { $0 + $1 }
// squares = [1, 4, 9, 16]
// sum = 10
2. filter(_:)
4. compactMap(_:)
📌 Purpose:
Returns elements of a collection that 📌 Purpose:
satisfy a condition. Maps over a collection and removes any nil values.
✅ Example: ✅ Example:
let numbers = [1, 2, 3, 4, 5] let strings = ["1", "abc", "3"]
let evenNumbers = numbers.filter { $0 % 2 == 0 } let numbers = strings.compactMap { Int($0) }
// evenNumbers = [2, 4] // numbers = [1, 3]
5. flatMap(_:) 7. sort(by:) / sorted(by:)
📌 Purpose: 📌 Purpose:
Used to flatten a collection of collections. Sorts a collection based on a comparison function.
✅ Example: ✅ Example:
let array = [[1, 2], [3, 4], [5]] let numbers = [5, 2, 9, 1]
let flatArray = array.flatMap { $0 } let sortedNumbers = numbers.sorted(by: <)
// flatArray = [1, 2, 3, 4, 5] // sortedNumbers = [1, 2, 5, 9]
6. forEach(_:) 8. contains(where:)
📌 Purpose: 📌 Purpose:
Performs an operation on each element of the
collection. Checks if any element of a collection satisfies a condition.
✅ Example: ✅ Example:
let names = ["Alice", "Bob", "Charlie"] let numbers = [1, 2, 3, 4]
names.forEach { print($0) } let hasEven = numbers.contains { $0 % 2 == 0 }
// Prints each name // hasEven = true
9. allSatisfy(_:) 11. drop(while:)
📌 Purpose: 📌 Purpose:
Returns true if all elements satisfy a given Drops (skips) elements from the start of a
condition. collection while a condition is true, then returns
✅ Example: the rest.
let numbers = [2, 4, 6]
✅ Example:
let areAllEven = numbers.allSatisfy { $0 % 2 == 0 } let numbers = [2, 4, 6, 7, 8, 10]
// areAllEven = true let dropped = numbers.drop(while: { $0 % 2 == 0 })
// dropped = [7, 8, 10]
10. first(where:)
12. prefix(while:)
📌 Purpose:
📌 Purpose:
Finds the first element that matches a condition. Returns a subsequence of elements from the start
✅ Example: while a condition is true
✅ Example:
let numbers = [1, 3, 4, 6]
let numbers = [2, 4, 6, 7, 8, 10]
let firstEven = numbers.first { $0 % 2 == 0 }
let prefix = numbers.prefix(while: { $0 % 2 == 0 })
// firstEven = 4
// prefix = [2, 4, 6]
13. zip(_:_:) 15 partition(by:)
📌 Purpose: 📌 Purpose:
Combines two sequences into a sequence of pairs
(tuples). Can be used with closures for Splits the array in-place by moving all elements
transformations. that don’t match a condition to the front, and the
matching ones to the end. Returns the index where
✅ Example: the matching part starts.
let names = ["Kalai", "Selvan"]
let scores = [85, 92, 78]
✅ Example:
for (name, score) in zip(names, scores) { var numbers = [5, 2, 9, 1, 7]
print("\(name): \(score)") let index = numbers.partition(by: { $0 % 2 == 0 })
} // numbers might be reordered to [5, 9, 1, 7, 2]
//Kalai: 85 // index = 4 (start of even numbers)
//Selvan: 92 print(numbers) // The order within partitions is not
14. removeAll(where:) guaranteed
print(index) // 4
📌 Purpose: Removes all elements from a collection that
satisfy the given predicate.
✅ Example: Presented by:
var numbers = [1, 2, 3, 4, 5, 6] Kalaiselvan N (iOS Developer)
numbers.removeAll(where: { $0 % 2 == 0 })
// numbers = [1, 3, 5]
📧 kalaikmv@gmail.com