Understanding Higher Order Functions in Swift 3

A Higher Order Function is just a special name for a function that accepts one or more functions as it’s arguments. I know, It sounds a lot more complicated than that, but it is actually quite simple. These functions are nothing new and have been a key part of functional languages for quite some time now. They have many advantages, some of the more popular ones including brevity, understandability, and easy re-use. Swift includes many Higher Order Functions out of the box, but developers have the ability to create their own as well.

What Makes Higher Order Functions So Special?

Let’s say we need to increment each integer in a array. Here is how we would do this procedurally.

And here is how we would do it declaratively using the Map function, a Higher Order Function that is part of the Swift standard library which allows us to transform individual elements of an array and create a new array from them in a more elegant way than we would procedurally.

As I’m sure you can already tell, the declarative example accomplishes the task in a conciser way than the imperative example. This is because Imperative style focuses on the “How”(Steps to be taken to get the result.), while Declarative style focuses on the “What”(Expression of what needs to happen.). Now let’s see how each of these styles approached our problem. In the imperative example, we approached the problem as if we were the computer — “First, I am going to create an array to hold the modified numbers, then I am going to iterate through all the numbers in the passed in array and increment each one, then I am going to append each incremented number to my new array, and finally I will return the modified numbers”. This is very different from the declarative example where we approach the problem on a level that is more understandable for humans. We focused on expressing what needs to happen and leave everything else for the computer to take care of. Humans are happier thinking like humans.

Creating Higher Order Functions

Now let’s get a better understanding of how Higher Order Functions work by creating our own. Here is an example implementation of the map function.

In Swift, the Map function is an extension of the Sequence protocol and is a bit different than my implementation, but for the sake of this example I have made this implementation simpler.

This implementation of the Map function is passed a transform function which each element of the array is passed into. Element represents a placeholder for the type that the array holds. T represents the placeholder after the element has been passed into the transform function and it’s type is determined by the return type of the transform function. In the end, the Map function returns the transformed elements.

Examples

Using the Map function to perform a FizzBuzz Test
Using the Filter function to return all even numbers from an array
Using the Reduce function to calculate the sum of all numbers in the array starting at the number 4
Using the Sorted function to sort the numbers array in descending order
Chaining the Filter and Reduce functions to calculate the sum of the numbers in the array that are less than or equal t0 50