In Swift we have map, flatMap and now compactMap. What do each of these functions do and how can we use them? Let’s have a look at map

Part 1:  Map

The map function takes a collection (often an array) and a function, and returns an array of items from the collection with the function applied to each item:

So for example, we could take an array of integers and a function which multiplies integers by 2:

Or given a list of names, we could capitalise them all:

[NB: If the curly brackets or $0 notation are not familiar then it’s worth reading up on closures before continuing]

The same result can be achieved by looping through each element in the array as follows:

So why use the map function?

  • map is declarative. We are declaring what we want the code to do, rather than how we want it to do it (imperative). The code is therefore declaring our intent, so should be clearer and easier to understand.
  • With map we could declare the resulting array withlet whereas with a loop we need to use var.  If we don’t want this array to change after we’ve made it then this is safer code.

Next up flatMap… coming soon!