flatMap and Chaining
Flatten nested collections and chain transforms.
flatMap and Chaining is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Faces of flatMap
flatMap on a sequence flattens one level of nesting. It applies a closure that returns a sequence for each element, then concatenates all those sequences into one flat array.
Flattening Nested Arrays
Turn an array of arrays into a single array:
let nested = [[1, 2], [3, 4], [5]]
let flat = nested.flatMap { $0 }
print(flat) // [1, 2, 3, 4, 5]flatMap That Produces Sequences
Each element can expand into many:
let nums = [1, 2, 3]
let expanded = nums.flatMap { [$0, $0 * 10] }
print(expanded) // [1, 10, 2, 20, 3, 30]Pairing Every Combination
flatMap is handy for cross products:
let suits = ["Hearts", "Spades"]
let ranks = ["A", "K"]
let deck = suits.flatMap { suit in ranks.map { "\($0) of \(suit)" } }
print(deck) // ["A of Hearts", "K of Hearts", "A of Spades", "K of Spades"]flatMap vs compactMap
Historically flatMap also unwrapped optionals, but that role moved to compactMap. Today:
flatMap— flatten nested sequencescompactMap— dropnilfrom optional results
Flattening Words into Characters
Expand each string into its characters:
let words = ["hi", "yo"]
let chars = words.flatMap { Array($0) }
print(chars) // ["h", "i", "y", "o"]Joining Sub-results
Collect items from nested model objects:
struct Team { let members: [String] }
let teams = [Team(members: ["Ann", "Bo"]), Team(members: ["Cy"])]
let all = teams.flatMap { $0.members }
print(all) // ["Ann", "Bo", "Cy"]Chaining Transforms
The real power comes from chaining: map, filter, flatMap, and reduce read top to bottom as a data pipeline. Each step produces a new sequence for the next.
A Full Pipeline
Filter, flatten, transform, then sum:
let groups = [[1, 2, 3], [4, 5], [6]]
let result = groups
.flatMap { $0 }
.filter { $0 % 2 == 0 }
.map { $0 * $0 }
.reduce(0, +)
print(result) // 4 + 16 + 36 = 56flatMap then compactMap
Flatten first, then keep only the parseable values:
let rows = [["1", "x"], ["3", "4"]]
let nums = rows
.flatMap { $0 }
.compactMap { Int($0) }
print(nums) // [1, 3, 4]Keeping Chains Readable
Put each step on its own line and name intermediate results when a chain gets long. Readability beats squeezing everything into one expression.
let names = ["ann", "BO", "cy"]
let titled = names
.map { $0.lowercased() }
.map { $0.capitalized }
.sorted()
print(titled) // ["Ann", "Bo", "Cy"]Quick Check
What does [[1, 2], [3]].flatMap { $0 } return?
Recap
You learned flattening and chaining:
flatMapflattens one level of nested sequences- Use
compactMap(not flatMap) to drop optionals - Chain
filter/map/flatMap/reduceinto readable pipelines - One step per line keeps long chains clear
Course complete! Next: lazy sequences.
Frequently asked questions
Is the “flatMap and Chaining” lesson free?
Yes — the full text of “flatMap and Chaining” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “flatMap and Chaining”?
Flatten nested collections and chain transforms. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “flatMap and Chaining” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- map and compactMap
- filter and Predicates
- reduce and reduce(into:)
- flatMap and Chaining