From the course: Learning Functional Programming with Swift

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Mapping

Mapping - Swift Tutorial

From the course: Learning Functional Programming with Swift

Start my 1-month free trial

Mapping

[Instructor] The first of Swift's built-in functions that we're going to be talking about is called Map. Map is used when you want to take all the elements in an array and convert them to some other value. For example, if you wanted to double all the elements in an array of numbers or convert an array of inch measurements into an array of centimeter measurements. The way we do this is by calling Map on an array and passing Map some function that we want to apply to each element in the array. Map then returns another array that contains the return values of the function for each element. So, in other words, it takes each element and maps it to the return value of the function we give it. If this function is something like square, for example, we get an array where each of the numbers have been squared. There is one thing that's important to remember with the Map function and most of Swift's built-in array functions. In typical functional fashion, it doesn't actually change the array we…

Contents