What is Functional Programming?
Functional programming (FP) is a way of writing code that focuses on transforming data through reusable functions instead of changing state or using loops.
Itβs about breaking problems into small, predictable building blocks and combining them to get the result you want.
Think of it like assembling a burger:
- You start with ingredients (data).
- You apply steps in a fixed order (functions).
- You always get the same burger if the ingredients are the same (predictable output).
Functional programming is about writing code in small, reusable, and predictable stepsβjust like following a recipe.
β Functional programming focuses on declaring what should happen rather than how it should happen* (declarative programming).
β Higher-order functions take this declaration* and handle the procedural execution of data transformations behind the scenes.
π Further Reading: Wikipedia - Functional Programming
Key Principles of Functional Programming
- Pure Functions β Always return the same result for the same input, without modifying anything outside.
- Immutability β Once data is created, it doesnβt change (instead, you create new data).
- First-Class Functions β Functions are treated like variables (can be passed around and returned).
- Higher-Order Functions β Functions that take other functions as input or return them as output.
- Declarative Code β Focuses on what to do rather than how to do it (less manual control).
Example: Functional vs. Imperative
Imperative (Step-by-Step) Approach
var total = 0
let numbers = [1, 2, 3, 4, 5]
for number in numbers {
total += number
}
print(total) // Output: 15
- Uses mutable state (
total
changes) - Uses loops to control flow manually
Functional Approach
let numbers = [1, 2, 3, 4, 5]
let total = numbers.reduce(0, +)
print(total) // output: 15
- No mutation (no variable is reassigned)
- Uses higher-order function (
reduce
) - More readable and declarative
Functional Concepts in Swift
map
β Applies a transformation to each element in a collection.filter
β Removes elements that donβt meet a condition.reduce
β Combines elements into a single result.flatMap
β Flattens nested collections while applying a transformation.compactMap
β Removesnil
values while transforming the data.
Example: Transforming Data Functionally
let numbers = [1, 2, 3, 4, 5]
// Double each number
let doubled = numbers.map { $0 * 2 }
print(doubled) // Output: [2, 4, 6, 8, 10]
// Keep only even numbers
let evens = numbers.filter { $0 % 2 == 0 }
print(evens) // Output: [2, 4]
// Sum all numbers
let sum = numbers.reduce(0, +)
print(sum) // Output: 15
Key Takeaways
- Functional programming focuses on transforming data rather than modifying state
- Pure functions prevent unexpected changes, making debugging easier
- Immutability makes code safer and avoids unintended side effects
- Higher-order functions (
map
,filter
,reduce
) simplify how you process data