Lazy sequence is a special wrapper around a collection that allows deferring the execution of computations (filter, map, etc.) until actual access to the elements occurs. This means that operations on the lazy sequence will only be computed when their result is accessed (e.g., when calling .forEach, .first, converting to an array, etc.).
When to use:
Example:
let numbers = Array(1...1_000_000) let lazyNumbers = numbers.lazy.map { $0 * 2 }.filter { $0 % 3 == 0 } let first = lazyNumbers.first // The chain of operations will only be computed now for the first matching element!
Pitfalls:
What is the difference between calling
.map { ... }and.lazy.map { ... }on an array?
Answer:
.map { ... } applies the closure to each element and immediately returns a new array, meaning all elements will be processed and stored in memory..lazy.map { ... } returns not an array, but a lazy sequence (wrapper), which does not process elements immediately but only upon access.Example:
let a = Array(1...10) let eagers = a.map { $0 * 2 } // Array with 10 elements let laziers = a.lazy.map { $0 * 2 } // LazySequence, not containing results immediately
Story
In a large project, a developer applied a chain of several calls to map, filter, reduce on a huge array of data without .lazy. This led to temporarily allocating large arrays at each intermediate step, nearly doubling memory consumption and causing crashes on some low-RAM devices.
Story
A lazy sequence with a side effect in the inner closure (e.g., sending an event or printing inside map/filter) was used in a code block. The developer expected this operation to execute immediately, but the event did not happen at all — because the elements of the lazy sequence were never accessed, and the code with the event was not invoked at all. As a result, logs and metrics were inaccurate.
Story
In the case of collecting statistics from data in a large database, a lazy sequence was used in combination with multiple passes (e.g., looking for the first element twice, and then counting). Each pass over the lazy sequence initiated a full recalculation of operations — leading to a twofold slowdown and unnecessary strain on the system. The problem disappeared after switching to a regular array.