Higher-Order Functions

As iOS developers, we must aware of Higher-order functions. Using higher-order functions in our code enhances the execution speed of our code and speed up our development skills. A higher-order function can be defined as a function that accepts one or more functions as arguments and returns a function as a result.

In this article, we will discuss some swift higher-order functions, including forEach, map, CompactMap, flatMap, filter, reduce, sort, and sorted.

forEach function

The forEach iterates through all elements of an array and doesn't return anything to the compiler. Consider the following example in which we use forEach to iterate through an array of integers.

It prints the following output on the console.

10 20 21 65 32 21 
10 20 21 65 32 21

In the first statement, we have used the basic syntax of forEach() to print array integers. In the second statement, we use the shorthand syntax of forEach(). The forEach() works similarly to the for-in loop. However, we cannot use the break and continue statements to exit from the closures in forEach.

map

The map is used to iterate through all array elements and returns a modified array after performing some operations on each element of it. Consider the following example in which we have an array of names that include first and last names. We use the map function to split the last name from each name and assign it back to names.

It prints the following output on the console.

Output

["John", "David", "Mike", "Will"]
["John", "David", "Mike", "Will"]

compactMap

A compactMap iterates the array and returns an updated array that includes only the elements that satisfy the condition written inside the compactMap body. The element that doesn't satisfy the condition is excluded from the modified array.

Consider the following example in which we have an array of marks. The compactMap() returns the updated array, including those marks that can be converted to Integer.

It prints the following output on the console.

["10", "20"]
["10", "20"]

flatMap

The flatMap function is used to convert a 2-D array to a 1-D array. Consider the following example.

It prints the following output on the console.

["John", "Rockey", "David", "Smith", "Roy", "Max"]
["John", "Rockey", "David", "Smith", "Roy", "Max"]

filter

As the name suggests, the filter () is used to filter the array based on some particular condition. It iterates through an array and returns a modified array where elements satisfy the condition written inside the filter body.

Consider the following example in which we filter all the even and odd numbers in an array separately.

It prints the following output on the console.

[2, 4, 6, 8, 10, 12]
[3, 6, 9, 12]

reduce

The reduce () is used to iterate through all array elements and returns the object with a combined value of initial result and current result. Consider the following syntax to use reduce in the code.

Consider the following example in which we calculate the sum of all elements in an array by setting the initial result to 0.

Sort(by: ) and sorted(by: )

The sort (by:) and sorted (by:) functions are used to sort an array of elements based on some particular condition. However, the sorted (by:) returns a new array that contains the sorted elements. Consider the following example.

It prints the following output on the console.

Output

[3, 10, 12, 14, 17, 19, 52, 61, 82]
[3, 10, 12, 14, 17, 19, 52, 61, 82]
[3, 10, 12, 14, 17, 19, 52, 61, 82]
[3, 10, 12, 14, 17, 19, 52, 61, 82]





Latest Courses