Higher Order Function in Swift 3.0

Pandurang Yachwad
Apps Studio
Published in
2 min readMar 26, 2017

Swift has made programming lean and clean. Higher Order functions certainly makes writing complex tasks on collection types much easier and simple. This can be very good replacement for many “for” loops. Here are the four higher order functions which should be used on collection types regularly to write swift code.

Map: Map loops over the collection and applies the same operation to each element in the collection.

Let’s say, you want to have square of the items in the array called numberArray. Here is how we can do using “for” loop.

var numberArray = [1,3,7,10,12]
var squareArray: [Int] = []
for number in numberArray{
squareArray.append(number * number)
}
print(squareArray) // squareArray = [1, 9, 49, 100, 144]

Alternatively, it can be done simpler way using Map on array as below:

var numberArray = [1,3,7,10,12]
let squareArray = numberArray.map { $0 * $0 }
print(squareArray) // squareArray = [1, 9, 49, 100, 144]

Filter: Loops over the collection and return an array that contains elements that meet condition.

Let’s say, you want to filter even numbers only in the following array example. square of the items in the array called numberArray.Here is how we can do using “for” loop.

var numberArray = [1,2,3,4,5,6,7,10,12,14]
var evenArray: [Int] = []
for number in numberArray{
if (number % 2 == 0){
evenArray.append(number)
}
}
print(evenArray) // evenArray = [2, 4, 6, 10, 12, 14]

Alternatively, it can be done simpler way using filter on array as below:

var numberArray = [1,2,3,4,5,6,7,10,12,14]
let evenArray = numberArray.filter{ $0 % 2 == 0}
print(evenArray) // evenArray = [2, 4, 6, 10, 12, 14]

Reduce: It combines all the items in the collection to create a single value.

Let’s take example of string instead of integer this time. Say, you want to combine strings in below array. Here is how it’s done using for loop.

var stringArray = [“One”, “Two”, “Three”, “Four”]
var combinedString : String = “”
for string in stringArray{
combinedString.append(string)
}
print(combinedString) // combinedString = “OneTwoThreeFour”

Alternatively, it can be done using reduce function on the array.

var stringArray = [“One”, “Two”, “Three”, “Four”]
let combinedString = stringArray.reduce(“”, {$0 + $1})
print(combinedString) // combinedString = “OneTwoThreeFour”

FlatMap: When it’s implemented on the sequences, it flattens a collection of collections. It’s best case scenario is, removing nil from the collection.

  1. Let’s say you want to combine the nested array in below array. It’s done as below using for loop.

let numberArray = [[1,2,3,4,5],[7,8,9,10]]
var combinedArray: [Int] = [Int]()
for number in numberArray{
combinedArray = combinedArray + number
}
print(combinedArray) // combinedArray — [1, 2, 3, 4, 5, 7, 8, 9, 10]

Alternatively, it can be done using flatMap in single line of code.

let numberArray = [[1,2,3,4,5],[7,8,9,10]]
let combinedArray = numberArray.flatMap { $0 }
print(combinedArray) // combinedArray — [1, 2, 3, 4, 5, 7, 8, 9, 10]

Here are some advantages of using Higher Order Functions.

  • It helps to read and understand complex programming
  • Helps to write simple and short code and better readability
  • Improve the Swift language coding

Originally published at leapforwards.wordpress.com on March 26, 2017.

--

--

Pandurang Yachwad
Apps Studio

Mobile App Developer and hustler. Life is short, utilize to fullest. Just do it!