0Pricing
Swift Academy · Lesson

Building Custom Lazy Sequences

Create your own lazily-evaluated sequences.

Why Custom Lazy?

Beyond map and filter, you can build your own lazily-evaluated sequences for things like infinite generators or expensive computed streams that should run on demand.

The LazySequence Type

Calling .lazy wraps a collection in a LazySequenceProtocol conformer. Operations you add return more lazy wrappers:

let view = [1, 2, 3].lazy
let mapped = view.map { $0 * 2 }
print(Array(mapped))  // [2, 4, 6]  -- LazyMapSequence

All lessons in this course

  1. The lazy Property
  2. Lazy map and filter
  3. Lazy vs Eager Trade-offs
  4. Building Custom Lazy Sequences
← Back to Swift Academy