Functional programming (FP) builds programs out of functions: small, predictable pieces that take input, return output, and don’t secretly change anything else. Swift isn’t a purely functional language, but it borrows many of FP’s best ideas: first-class functions, closures, value types, map and filter, and a strong preference for immutability with let.
You don’t have to go all in to benefit. Even a little functional thinking makes code easier to test, easier to reason about, and safer to run concurrently.
This guide follows the path of objc.io’s excellent book Functional Swift, from the core mindset to advanced abstractions. The articles develop original examples with executable checks, explain the limits of each technique, and finish with exercises that extend the design.
The series uses a Swift 5.7 baseline, released on September 12, 2022. Article dates run from November 2022 through June 2024; the examples use language features and APIs available by those dates. You can follow the sections in order or start with an everyday collection problem and return to the underlying ideas.
Article available Coming soon
1. The Functional Mindset
Thinking Functionally in Swift: Functions as Values
A shipping calculator starts with one rule. Then express delivery arrives, followed by regional pricing and a promotion. If every variation becomes...
Pure Functions and Side Effects in Swift: Build a Testable Core
A reminder should be sent when an invoice is overdue, remains unpaid, and has not received a reminder today. That rule sounds...
From Imperative to Declarative Swift Without Losing the Algorithm
Replacing a loop with map does not automatically improve a program. A successful declarative refactor makes the calculation’s intent easier to see...
Wrapping Core Image Filters in Swift Functions
An image editor needs reusable recipes: blur then tint, tint then crop, or apply the same preset to a preview and an...
2. Everyday Functional Swift
Higher-Order Functions in Swift: Reusable Policies and Honest Effects
A higher-order function accepts a function, returns a function, or does both. Swift’s map is one example, but the idea reaches beyond...
map, filter, and reduce in Swift: Choose the Operation That Matches the Result
map transforms each element, filter selects elements, and reduce combines a sequence into an accumulated result. Their names are familiar, but choosing...
compactMap and flatMap in Swift: Missing Values, Nested Values, and Lost Errors
compactMap and flatMap are often taught as two ways to clean up a collection. That description hides the important distinction. One removes...
Optionals as a Functional Tool in Swift
An optional means a value may be absent. That small model supports a useful composition style: transform a present value, chain a...
Enumerations and Algebraic Data Types in Swift: Make Invalid States Harder to Build
A screen represented by isLoading, hasError, and an optional result has more states than it probably needs. Can it be loading and...
3. Immutability and Testing
The Value of Immutability in Swift: Stable Facts and Explicit Changes
Immutability reduces the number of things that can change while you are reasoning about a calculation. A checkout total is easier to...
Value Types and Copy-on-Write in Swift: Independence Without Eager Copies
Value semantics describe what a caller can observe: changing one value does not unexpectedly change another independent value. Copy-on-write describes an implementation...
Property-Based Testing in Swift: Generators, Shrinking, and Reproducible Failures
An example test says a function works for one chosen input. A property test states a relationship that should hold across a...
4. Functional Data Structures
Purely Functional Data Structures in Swift: Persistent Lists and Queues
A persistent data structure keeps earlier versions usable after an update. Here “persistent” describes version preservation, not saving to disk. An editor...
Persistent Binary Search Trees and Tries in Swift
An immutable tree update reconstructs the path to a changed node while reusing untouched branches. That gives the program a new root...
Iterators and Sequences in Swift: Pull Values Without Assuming Replay
A for loop asks an iterator for the next element until the iterator says it has finished. The sequence supplying that iterator...
Lazy Sequences in Swift: Deferred Work, Repeated Work, and Bounded Results
Adding .lazy can avoid intermediate arrays and skip transformations for elements the caller never needs. It can also defer effects, repeat expensive...
5. Bigger Ideas
Building Diagrams with Functions in Swift: A Small Layout Language
A drawing API often starts as a sequence of commands: move here, set a color, draw a rectangle, restore the context. That...
Parser Combinators in Swift: Build a Grammar from Small Functions
A parser consumes a prefix of input and returns both a value and the unconsumed suffix. A parser combinator builds a larger...
A Functional Spreadsheet App in Swift: Formulas, Dependencies, and Cycles
A spreadsheet is a useful test of functional design because it mixes several concerns: formula syntax, evaluation, dependency tracking, edits, and a...
Functors, Applicative Functors, and Monads in Practical Swift
The names sound abstract, but the operations are familiar. You transform an optional with map, combine independently validated fields, and chain a...
Up next
Data Structures & Algorithms