Swift Higher Order Functions

Here is what I like about Swift the most… it forces me to think in terms of what I can only describe as decoupled behavior. Behavior or implementation that doesn’t depend on any specific type. This can be clearly observed with higher order functions, protocol oriented programming or just Swift in general.

So what are they? Higher order functions are functions that take other functions as parameters. And even though the implementation of these functions can be, as any implementation, verbose and easily replicated, the advantage of using higher order functions is the fact that they promote a much succinct and easier to understand code by eliminating clutter. Some of these functions even have a default behavior (sort(_:) for example will sort elements in ascending order) which is very useful.

Swift standard library has 13 of these ready to use. Here they are:

As a reference, let’s discuss a couple of these functions as not every one of them needs its own write up (some of them are self explanatory).


Let’s assume we have an array of integers we want to convert into an array of strings. We can easily do that with a simple function that looks like this:

The problem with this approach is that if you ever need to convert an array of doubles, you’ll need to refactor current function to accept an array of AnyObject in addition to appropriately handling the implementation. But what if you need to go through an existing array of integers and calculate square root value for each number? The above function will not work at all. If there only was a way to write a single function that can do any type of transformationmap(_:) to the rescue.

As you can see we can use map(_:) to map an array of integers to an array of strings or an array of squared integers for that matter without bothering with somewhat duplicate implementation.

For the curious ones, here is how map(_:) implementation looks like:

Now since we can write our own map(_:), why would we bother with using provided ones? Like I’ve said before, they are written so we don’t have to ;] and they make our code cleaner, more readable, and easier to understand. I also believe they are written to nudge current cocoa developers into a different way of thinking about writing reusable code – but that’s just my opinion.


reduce(_:) function is used to combine elements into an aggregate value. For example, let’s assume we wanted to write a function that takes in a dictionary of parameters [String : AnyObject] and returns a query string we can append to our base URL when constructing a URL request. One of many implementations could look like this:

As you can see, this implementation works well. We combine dictionary values and keys and then simply trimming away the access “&” character. However, as you can already conclude, this implementation is very specific to this task and if we wanted to calculate points total from, lets say a dictionary of [String : Int]

we would need to write a new function… Alternatively, What we can do, is to use a generic, higher order function. Here is an example:

Alright, lets move along. There are a couple of other higher order functions I would like to mention.


Similarly to map(_:), flatMap(_:) allows for flattening multiple arrays for example into a single one. Here is an example:

Using flatMap(_:) in this case will first flatten our two arrays into a single one allowing us to use map(_:) to then compute square root of each integer. Pretty cool.


Use sort if you need to sort elements in a particular order. It’s worth mentioning that sort(_:) has a default behavior where the elements are sorted in ascending order. Here is an example.


Very useful when you you need to find out the index of an element. Function will return nil if value is not found.


The last higher order function I would like to mention is split(_:). Let’s say we have a string of characters and would like like to split that string into separate subsequences…

This should conclude this rather short post on Swift’s higher order functions. In conclusion, as you can see, they are very useful and are welcome addition to the Standard Library.

Swift Higher Order Functions

Leave a Reply

Your email address will not be published. Required fields are marked *