Lazy map and filter
Chain lazy transforms without intermediate arrays.
Lazy map and filter is a free Swift Academy lesson on CoddyKit — lesson 2 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.
Intermediate Arrays
Eager chains allocate a fresh array between every step. filter().map() on a big array builds two full arrays even though you may want only a few elements.
The Eager Allocation
Two intermediate arrays are created here:
let nums = Array(1...6)
let result = nums
.filter { $0 % 2 == 0 } // builds [2, 4, 6]
.map { $0 * 10 } // builds [20, 40, 60]
print(result) // [20, 40, 60]Fusing Steps with lazy
A lazy chain fuses the operations: each element flows through every step before the next element starts, so no intermediate array is ever allocated.
A Lazy Chain
Same logic, zero intermediate arrays:
let nums = Array(1...6)
let result = nums.lazy
.filter { $0 % 2 == 0 }
.map { $0 * 10 }
print(Array(result)) // [20, 40, 60]Element-by-Element Flow
Tracing shows interleaved execution rather than stage-by-stage:
let out = [1, 2, 3].lazy
.filter { n -> Bool in print("filter \(n)"); return n > 1 }
.map { n -> Int in print("map \(n)"); return n }
print(Array(out)) // filter 1, filter 2, map 2, filter 3, map 3 -> [2, 3]Early Exit Saves Work
Combine a lazy chain with first(where:) to stop as soon as you can:
let nums = Array(1...1_000_000)
let hit = nums.lazy.map { $0 + 1 }.first { $0 > 5 }
print(hit!) // 6 -- only a handful of elements processedLazy with prefix
Take just the first few results of an expensive transform:
let squares = (1...).lazy.map { $0 * $0 }
print(Array(squares.prefix(5))) // [1, 4, 9, 16, 25]Counting Skipped Work
A counter proves how few elements run:
var transforms = 0
let result = Array(1...100).lazy
.map { (n: Int) -> Int in transforms += 1; return n * 2 }
.prefix(3)
_ = Array(result)
print(transforms) // 3, not 100Lazy compactMap
compactMap has a lazy form too:
let raw = ["1", "x", "3", "y", "5"]
let first = raw.lazy.compactMap { Int($0) }.first
print(first!) // 1Mixing Many Steps
Long fused pipelines stay allocation-free until materialized:
let r = Array(1...20).lazy
.filter { $0 % 2 == 0 }
.map { $0 * $0 }
.filter { $0 > 50 }
print(Array(r)) // [64, 100, 144, 196, 256, 324, 400]When Fusion Helps Most
Lazy fusion is a win when the source is large and you keep only part of the result. If you consume the whole thing into an array anyway, the savings shrink and eager may read cleaner.
let big = Array(1...10_000)
let sample = Array(big.lazy.filter { $0 % 1000 == 0 })
print(sample) // [1000, 2000, ... 10000]Quick Check
What does a lazy filter().map() chain avoid that an eager one creates?
Recap
You learned lazy chains:
- Eager chains allocate an array between every step
.lazyfuses steps — element-by-element, no intermediates- Pairs perfectly with
first(where:)andprefixfor early exit - Biggest win: large source, partial consumption
Next: lazy vs eager trade-offs.
Frequently asked questions
Is the “Lazy map and filter” lesson free?
Yes — the full text of “Lazy map and filter” 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 “Lazy map and filter”?
Chain lazy transforms without 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 4, so you can start here or from the beginning and move at your own pace.
How long does the “Lazy map and filter” 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
- The lazy Property
- Lazy map and filter
- Lazy vs Eager Trade-offs
- Building Custom Lazy Sequences