Custom iterators & performance tips
Implement custom iterators/sequences and keep them efficient: single-pass behavior, avoiding extra allocations, and using lazy views.
Custom iterators & performance tips is a free Swift Academy lesson on CoddyKit. This is lesson 3 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.
Custom iterators 101
Create your own IteratorProtocol and Sequence. Iterators are usually single-pass and must return nil at the end.
IteratorProtocol example
Implement next() to yield one element per call and advance state; return nil to finish.
struct CountdownIterator: IteratorProtocol {
var current: Int
mutating func next() -> Int? {
guard current >= 0 else { return nil }
defer { current -= 1 } // advance state
return current
}
}
var it = CountdownIterator(current: 3)
while let n = it.next() { print(n, terminator: " ") } // 3 2 1 0
print("")Sequence wrapper
A Sequence provides makeIterator(). Now you can use for-in, map, filter, etc.
struct Countdown: Sequence {
let start: Int
func makeIterator() -> CountdownIterator {
CountdownIterator(current: start)
}
}
for n in Countdown(start: 5) {
print(n, terminator: " ") // 5 4 3 2 1 0
}
print("")AnyIterator shortcut
AnyIterator/AnySequence let you create iterators quickly with a closure—handy for lightweight generators.
// Build an iterator from a closure
var i = 1
let anyIt = AnyIterator<Int> {
guard i <= 3 else { return nil }
defer { i += 1 }
return i * 10
}
let numbers = AnySequence(anyIt)
for x in numbers { print(x, terminator: " ") } // 10 20 30
print("")Lazy to save work
Prefer .lazy when only part of a pipeline is needed; it saves allocations and work.
let big = Array(1...10_000)
// Eager: builds two intermediate arrays
let eager = big.map { $0 * 2 }.filter { $0 % 3 == 0 }
// Lazy: no temporaries; work is done during collection
let lazyView = big.lazy.map { $0 * 2 }.filter { $0 % 3 == 0 }
let result = Array(lazyView.prefix(5)) // only first 5 actually computed
print(result)Perf tips
Tips:
- Keep iterators simple—advance state and return
nilat end. - Use .lazy to avoid intermediate arrays.
- Reserve capacity for results when you know the size (
res.reserveCapacity(n)). - Prefer Collection algorithms when you need multi-pass or indices.
Iterator rule
Quick check: What must next() do at the end of iteration?
Recap
Recap: Write a minimal IteratorProtocol, wrap it in a Sequence, and keep pipelines efficient with .lazy and preallocation when possible.
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 “Custom iterators & performance tips” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Custom iterators & performance tips”, 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 “Custom iterators & performance tips”?
Implement custom iterators/sequences and keep them efficient: single-pass behavior, avoiding extra allocations, and using lazy views. 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 3 of 3.
How long does the “Custom iterators & performance tips” 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