0Pricing
Swift Academy · Lesson

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 — lesson 3 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.

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 nil at 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.

Frequently asked questions

Is the “Custom iterators & performance tips” lesson free?

Yes — the full text of “Custom iterators & performance tips” 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 “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; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

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

  1. Sequence/Collection hierarchies
  2. Map/Filter/Reduce, lazy sequences
  3. Custom iterators & performance tips
← Back to Swift Academy