Using closures with Swift's array algorithms

In Chapter 3, Using Collections and Cocoa Data Types, we looked at several built-in algorithms that we could use with Swift's arrays. In that chapter, we briefly saw how to add simple rules to each of these algorithms with very basic closures. Now that we have a better understanding of closures, let's see how we can expand on these algorithms using more advanced closures.

In this section, we will primarily be using the map algorithm for consistency purposes; however, we can use the basic ideas demonstrated with any of the algorithms. We will start by defining an array to use:

let guests = ["Jon", "Kim", "Kailey", "Kara"]

This array contains a list of names and the array is named guests. This array will ...

Get Mastering Swift 2 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.