A sequence on which normally-eager operations such as map and filter are implemented lazily.
SDK
Xcode 8.0+
Framework
Swift Standard Library
Declaration
Overview
Lazy sequences can be used to avoid needless storage allocation and computation, because they use an underlying sequence for storage and compute their elements on demand. For example,
is a sequence containing { 2, 4, 6 }. Each time an element of the lazy sequence is accessed, an element of the underlying array is accessed and transformed by the closure.
Sequence operations taking closure arguments, such as map and filter, are normally eager: they use the closure immediately and return a new array. Using the lazy property gives the standard library explicit permission to store the closure and the sequence in the result, and defer computation until it is needed.
To add new lazy sequence operations, extend this protocol with methods that return lazy wrappers that are themselves LazySequenceProtocols. For example, given an eager scan method defined as follows
we can build a sequence that lazily computes the elements in the result of scan:
and finally, we can give all lazy sequences a lazy scan method:
Returns a LazyMapSequence over this Sequence. The elements of the result are computed lazily, each time they are read, by calling transform function on a base element.