Before we can dive into the code, you need to be familiar with closures. It’s necessary for this article otherwise you will be completely lost…
If everything is ok with that, let’s go!
The map method
This one is the easy one. Every other method we are going to deal with is based on this one.
This method will only loop over each element of your collection and apply the same function on it.
Here is an example:
In this example, I just multiply every number of the array by two.
Of course, it also works with all types of collections such as arrays, sets, tuples and there is a small difference with dictionaries.
The difference is that with dictionaries, the map needs to take a second parameter which is the key to the value passed in the closure:
If you want to have access to the index of the array when looping over it, you just call the enumerated function and you chain it with the map function and then you use map as if you were using it on a dictionary.
The filter method
As I said, this method uses the same logic as the map function but instead of applying a function to each element in the collection, we apply them a condition.
The closure must return a boolean so telling whether or not the element a given element should stay in the collection or not.
In this example, I only keep the values that are greater or equal to 18.
As you may have guessed, it works on every type of collections but if you use a dictionary, you will have to pass a second parameter for the key.
With dictionaries, you can also filter using the key: the closure only needs to return a boolean!
The reduce method
This one is a bit more tricky but once you understand how it works, it can be extremely useful.
The reduce method creates a single value when looping over an array.
For example, you can use it to sum all the numbers of a collection or you can also concatenate all the strings of a collection.
This works like the map method but with 2 parameters in the closure.
The first one is the value that stores the future result that will be returned once the loop over the collection is over. The second one is the current value when looping over a collection (just like in the previous examples).
Here is an example:
At the end of this code, sum is equal to the sum of all the values in the prices array.
At every iteration, we add the current value to the result variable in the closure. The first parameter of the function is the starting value for the result.
- First iteration: the closure returns 0.0 + 19.0
- Second iteration: the closure returns 19.0 + 20.99
- Third iteration: the closure returns 39.99 + 3.50
If you want to use it on a dictionary, the second parameter will be a tuple of the key and the value for every iteration.
The flatMap method
This one is just like the map function but it flattens all the collections nested into the one you are looping over.
It means that if there is an array into an array, the flatMap function will loop over the array and the nested array and apply the closure passed as a parameter and will return an array of values without any nested array.
Be careful: it also flattens the strings. If you have an array of strings and you use flatMap on it, you will have an array containing every character of every string separated into a single value.
The compactMap method
This is exactly the same function as the flatMap function but instead of flattening the collection, it removes all the nil values and it only executes the code on the non-nil values.
Don’t hesitate to use these methods, they are extremely powerful when you have to deal with big collection types for example when you fetch data from an API.