Swift Academy · Lesson

Map/Filter/Reduce, lazy sequences

Use map / filter / reduce to transform, select, and combine; then add .lazy to defer work and avoid intermediate arrays.

Lesson 2 of 38 steps

Map/Filter/Reduce, lazy sequences is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 3. 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 3 lessons in total.

Core idea

Core algorithms:

  • map: transform each element
  • filter: keep elements matching a predicate
  • reduce: combine into one value
  • .lazy: defer work to avoid temporary arrays

map

map transforms each element and returns a new array.

let nums = [1, 2, 3]
let doubled = nums.map { $0 * 2 }        // [2, 4, 6]
print(doubled)

let words = ["swift", "kit"]
let upper = words.map { $0.uppercased() } // ["SWIFT","KIT"]
print(upper)

filter

filter keeps elements where the predicate returns true.

let scores = [45, 72, 88, 53]
let passing = scores.filter { $0 >= 60 } // [72, 88]
print(passing)

let names = ["Ana","Li","Max"]
let short = names.filter { $0.count <= 2 } // ["Li"]
print(short)

reduce

reduce folds a sequence into a single result using a start value and a combining closure.

let total = [1,2,3,4].reduce(0) { acc, x in acc + x }   // 10
print(total)

let joined = ["a","b","c"].reduce("") { $0 + $1 }             // "abc"
print(joined)

Lazy on-demand work

.lazy builds a deferred pipeline: operations run as you consume elements (e.g., via prefix, first, or iteration).

let range = 1...10
let lazyPipeline = range.lazy
    .map { x in print("map", x); return x * x }
    .filter { x in print("filter", x); return x % 2 == 0 }

// Only the first 3 results are needed:
let firstThree = Array(lazyPipeline.prefix(3))
print("result:", firstThree)
// Notice how only the work needed for 3 elements ran.

Tips

Tips:

  • Chain map → filter → reduce for clear data flows.
  • Add .lazy when you consume only part of a pipeline or want to avoid temporaries.
  • Materialize with Array(...) only when you need a concrete array.

lazy semantics

Quick check: What is the effect of .lazy on a map/filter chain?

Recap

Recap: Use map to transform, filter to select, reduce to combine. Add .lazy for on-demand evaluation and fewer allocations.

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 “Map/Filter/Reduce, lazy sequences” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Map/Filter/Reduce, lazy sequences”, 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 3 lessons in total.

What will I learn in “Map/Filter/Reduce, lazy sequences”?

Use map / filter / reduce to transform, select, and combine; then add .lazy to defer work and avoid intermediate arrays. 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 2 of 3.

How long does the “Map/Filter/Reduce, lazy sequences” 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. Sequence/Collection hierarchies
  2. Map/Filter/Reduce, lazy sequences
  3. Custom iterators & performance tips
← Back to Swift Academy