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] -- LazyMapSequenceAll lessons in this course
- The lazy Property
- Lazy map and filter
- Lazy vs Eager Trade-offs
- Building Custom Lazy Sequences