top of page
  • iamdevpatel58

Higher order functions in Swift

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


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


Map


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.

1 let celciusArray = [-10.0, -2.0, 10.0, 56.0, 87.0, 100.0]

2 let fahrenheitArray = celciusArray.map { $0 * (9/5) + 32 }

3 print(fahrenheitArray)

4 //Output: [14.0, 28.4, 50.0, 132.8, 188.6, 212.0]

5 Another example using an array of strings,

6 let stringArray = [“Egypt”, “Ghana”, “Haiti”, “India”, “Italy”]

7 let uppercasedStringArray = stringArray.map {$0.uppercased()}

8 print(uppercasedStringArray)

9 //Output: [“EGYPT”, “GHANA”, “HAITI”, “INDIA”, “ITALY”]


CompactMap


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


1 let possibleNumbers = [“1”, “2”, “three”, “///4///”, “5”]

2 let compactMapped: [Int] = possibleNumbers.compactMap { str in Int(str) }

3 print(compactMapped)

4 //Output: [1, 2, 5]

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

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

1 let stringArray = [“Khandvi”, “Samosa”, “Dhokla”, “Khakhra”,”Handvo”, “Idli”]

2 let filteredArray = stringArray.filter { $0.count <= 6 }

3 print(filteredArray)

4 //Output: [“Samosa”, “Dhokla”, “Handvo”, “Idli”]

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


Reduce

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.


1 let numbers = [1, 2, 3, 4]

2 let numberSum = numbers.reduce(0, { x, y in

3 x + y

4 })

5 print(numberSum)

6 //Output: 10

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


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.


1 let stringArray = [“Egypt”, “Ghana”, “Haiti”, “India”, “Italy”]

2 let sortedArray = stringArray.sorted()

3 print(sortedArray)

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

5 let sortedArray = stringArray.sorted(by: >)

6 print(sortedArray)

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

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

Chaining

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.

1 let stringArray = [“North America”, “South America”, “Antarctica”, “Europe”, “Asia”, “Africa”, “Australia”]

2 let result = stringArray.filter{$0.first == “A”}.map{$0.uppercased()}.sorted()

3 print(result)

4 //Output: [“AFRICA”, “ANTARCTICA”, “ASIA”, “AUSTRALIA”]

Contains

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.

1 let stringArray = [“Egypt”, “Ghana”, “Haiti”, “India”, “Italy”]

2 print(stringArray.contains(“Haiti”))

3 //Output: “true”

4 print(stringArray.contains(“China”))

5 //Output: “false”


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


AllSatisfy


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,

1 let names = [“Egypt”, “Ghana”, “Haiti”, “India”, “Italy”]

2 let allHaveAtLeastFive = names.allSatisfy({ $0.count >= 5 })

3 print(allHaveAtLeastFive)

4 //Output: true


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.


1 var names = [“Egypt”, “Ghana”, “Haiti”, “India”, “Italy”]

2 if let i = names.firstIndex(of: “Ghana”) {

3 names[i] = “China”

4 }

5 print(names)

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

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”,

1 if let i = names.firstIndex(where: { $0.hasPrefix(“G”) }) {

2 print(“\(names[i]) starts with ‘G’”)

3 }

4 //Output: Ghana starts with ‘G’


RemoveAll

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,

1 var numbers = [5, 6, 7, 8, 9, 10, 11]

2 numbers.removeAll(where: { $0 % 2 == 1 })

3 print(numbers)

4 //Output: [6, 8, 10]

The following two tabs change the content below.


1 view0 comments
bottom of page