Swift Academy · Lesson

flatMap and Chaining

Flatten nested collections and chain transforms.

Lesson 4 of 413 steps

flatMap and Chaining is a free Swift Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

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 sequences
  • compactMap — drop nil from 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 = 56

flatMap 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:

  • flatMap flattens one level of nested sequences
  • Use compactMap (not flatMap) to drop optionals
  • Chain filter / map / flatMap / reduce into readable pipelines
  • One step per line keeps long chains clear

Course complete! Next: lazy sequences.

Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “flatMap and Chaining” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “flatMap and Chaining”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

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

  1. map and compactMap
  2. filter and Predicates
  3. reduce and reduce(into:)
  4. flatMap and Chaining
← Back to Swift Academy