Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Learning Swift
  • Table Of Contents Toc
  • Feedback & Rating feedback
Learning Swift

Learning Swift

By : Andrew J Wagner
4.8 (5)
close
close
Learning Swift

Learning Swift

4.8 (5)
By: Andrew J Wagner

Overview of this book

If you are looking to build iOS or OS X apps using the most modern technology, this book is ideal for you. You will find this book especially useful if you are new to programming or if you have yet to develop for iOS or OS X.
Table of Contents (13 chapters)
close
close
12
Index

Lazy evaluation


In the previous chapter, we talked about lazily calculated properties on types. These were properties that were not calculated until they were first accessed. We can bring this same concept into functional programming and it actually becomes even more powerful.

First, it is important to realize the order in which these functions are executed. For example, if we only want the first element of our numbers mapped to strings:

var firstString = numbers.map({"\($0)"}).first

This works well except that we actually converted every number to a string to get to just the first one. This is because each step of the chain is completed in its entirety until the next one can be executed. To prevent this, Swift has a built-in function called lazy.

Essentially, lazy allows each element to flow through a series of functions one at a time, as needed. You use it to convert a normal list into a lazy list:

firstString = lazy(numbers).map({"\($0)"}).first

Now, instead of calling map directly on the numbers...

Unlock full access

Continue reading for free

A Packt free trial gives you instant online access to our library of over 7000 practical eBooks and videos, constantly updated with the latest in tech
bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY