Higher order functions in Swift


Higher-order functions are functions that can accept other function or closure as parameters. I’ll introduce some very useful higher-order functions.

Those functions are Map, Filter, Reduce, Sorted, CompactMap, Chaining, Contains. Other new higher-order functions were introduced in Swift 4.2. Those functions are AllSatisfy, FirstIndex, LastIndex and RemoveAll.


Map function can be used to loop over a collection and apply the same operation to each element in the collection.

Let’s assume we need to convert each random Celsius to Fahrenheit values. In these examples above map accepted a closure as an argument and returned the result in the new array that contains Fahrenheit values.


CompactMap function returns an array containing the non-nil results of calling the given transformation with each element of this sequence.

If we used map on possibleNumbers the result would be [1, 2, nil, nil, 5] but because we are using compactMap so the result is [1, 2, 5].


Filter function returns an array containing, in order, the elements of the sequence that satisfy the given predicate.

In this example, the filter is used to get only names shorter than or equal to six characters.


Reduce function is used to combine all items in a collection to create a single new value. The reduce method takes two values, an initial value and a combined closure.

When numbers.reduce is called, the following steps occur,

– The next partial result closure is called with initial results 0 in this case and the first element of numbers, returning the sum: 1.

– The closure is called again repeatedly with the previous call’s return value and each element of the sequence.

– When the sequence is exhausted, the last value returned from the closure is returned to the caller.


Sorted function returns the elements of the sequence, sorted.

Here’s an example of sorting a string array. Strings in Swift conform to the Comparable protocol, so strings are sorted in ascending order according to the less-than operator.

To sort the elements of your sequence in descending order, pass the greater-than operator to the sorted(by the : ) method.

let sortedArray = stringArray.sorted(by: >)


//Output: [“Italy”, “India”, “Haiti”, “Ghana”, “Egypt”]


Chaining is the ability to combine all those higher-order functions. Let’s filter out names that start with “A”, make them uppercase and then sort them.


Contains function is to return a boolean value indicating whether the sequence contains an element that satisfies the given predicate.

This example checks to see whether a string is in a stringArray.

Below is a new collection of higher order functions introduced in swift 4.2:


AllSatisfy function returns a boolean value indicating whether every element of a sequence satisfies the given predicate.The following code uses this method to test whether all the names in an array have at least five characters,

FirstIndex and LastIndex

FirstIndex(where:) and firstIndex(of:) that return the first index where the specified value appears in the collection. We have also lastIndex(of:) and lastIndex(where:) that returns the last index in which an element of the collection satisfies the given predicate.

This example shows how you can modify one of the names in an array of names.

You can use the predicate to find an element of a type that doesn’t conform to the equitable protocol or to find an element that matches particular criteria. Here’s an example that finds a name that begins with the letter “G”,


Now we have a new method removeAll(where:) that removes all the elements that satisfy the given predicate.

Use this method to remove every element in a collection that meets particular criteria. This example removes all the odd values from an array of numbers,

The following two tabs change content below.
I'm a software developer at DEV IT, keen to learn new technologies to develop scalable as well as high-performance software and work towards challenging roles in the field of Information Technology is something that I always look forward to.

Latest posts by Harsh Mehta (see all)

Leave a Reply

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