Using closures with Swift arrays
In Chapter 5, Using Swift Collections, we looked at several built-in algorithms that we can use with Swift's arrays. In that chapter, we briefly looked at 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 look at how we can expand on these algorithms using more advanced closures.
Using closures with Swift's array algorithms
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", "Kailey", "Kara"]
This array contains a list of names and the array is named guests
. This array will be used for the majority of the examples in this section. Now that we have our guests
array, let's add a closure that will print a greeting to each of the...