-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Learning Swift Second Edition
By :

A powerful feature of Swift is the ability to make these operations lazily evaluated. This means that, just like a lazy person would do, a value is only calculated when it is absolutely necessary and at the latest point possible.
First, it is important to realize the order in which these methods are executed. For example, what if we only want the first element of our numbers to be mapped to strings:
var firstString = numbers.map({String($0)}).first
This works well, except that we actually converted every number to a string to get to just the first one. That is because each step of the chain is completed in its entirety before the next one can be executed. To prevent this, Swift has a built-in method called lazy.
Lazy creates a new version of a container that only pulls specific values from it when it is specifically requested. This means that lazy essentially allows each element to flow through a series of functions one at a time, as it is needed. You can think about it like...
Change the font size
Change margin width
Change background colour