Map/Filter/Reduce, lazy sequences
Use map / filter / reduce to transform, select, and combine; then add .lazy to defer work and avoid intermediate arrays.
Map/Filter/Reduce, lazy sequences is a free Swift Academy lesson on CoddyKit — lesson 2 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “Map/Filter/Reduce, lazy sequences” lesson free?
Yes — the full text of “Map/Filter/Reduce, lazy sequences” is free to read here on the web, and the Swift Academy course includes 3 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 “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; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
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
- Sequence/Collection hierarchies
- Map/Filter/Reduce, lazy sequences
- Custom iterators & performance tips