Swift Functional Programming(Second Edition)
上QQ阅读APP看书,第一时间看更新

Function types

Along with, classes, structs, enums, and protocols, functions can also be used as types in Swift. In this section, we will explore how we can define functions as types and how we can use function types. Let's first discover what the type of the function is and how it is defined.

A function parameter type along with its return type defines the type of the function. For instance, the function type for the following coding example is (Int, Double) -> String:

func functionName(firstParam: Int, secondParam: Double) -> String 

We will be able to use function types in the way we use other types. The following code example presents a function type:

var simpleMathOperator: (Double, Double) -> Double 

Here, simpleMathOperator is a variable of a function of the (Double, Double) -> Double type. In other words, simpleMathOperator stores a function that accepts two Double parameters and returns a Double value.

To make it a little easier to read, we could define typealias for the function type as follows:

typealias SimpleOperator = (Double, Double) -> Double 

We can use this typealias in the simpleMathOperator definition as follows:

var simpleMathOperator: SimpleOperator 

We can define functions with the same type and assign them to our simpleMathOperator. The type of function in the following code snippet is (Double, Double) -> Double, which is in fact SimpleOperator:

func addTwoNumbers(a: Double, b: Double) -> Double { return a + b } 

func subtractTwoNumbers(a: Double, b: Double) -> Double { return a - b }

func pideTwoNumbers(a: Double, b: Double) -> Double { return a / b }

func multiplyTwoNumbers(a: Double, b: Double) -> Double { return a * b }

Therefore, we are able to assign these functions to simpleMathOperator as follows:

simpleMathOperator = multiplyTwoNumbers 

This means that simpleMathOperator refers to the multiplyTwoNumbers function:

let result = simpleMathOperator(3.0, 4.0) // result is 12 

As the other three functions also have the same function type, we will be able to assign them to the same variable:

simpleMathOperator = addTwoNumbers 
let result2 = simpleMathOperator(3.5, 5.5) // result is 9

We can use SimpleOperator as a parameter type of other functions:

func calculateResult(mathOperator: SimpleOperator, 
a: Double,
b: Double) -> Double {
return mathOperator(a, b)
}

print("The result is \(calculateResult(mathOperator: simpleMathOperator, a: 3.5, b: 5.5))") // prints "The result is 9.0"

Here, the calculateResult function has three parameters. The mathOperator parameter is a type of function type. The a and b parameters are Double. When we call this function, we pass a simpleMathOperator function and two Double values for a and b.

It is important to know that we pass only a reference to simpleMathOperator and this is not going to execute it. In the function body, we use this function and call it with a and b.

We can use SimpleOperator as a return type of a function as follows:

func choosePlusMinus(isPlus: Bool) -> SimpleOperator { 
return isPlus ? addTwoNumbers : subtractTwoNumbers
}

let chosenOperator = choosePlusMinus(isPlus: true)
print("The result is \(chosenOperator(3.5, 5.5))") // prints "The result is 9.0"

Here, the choosePlusMinus function has a Bool parameter; in its body, it checks for this parameter and returns addTwoNumbers or subtractTwoNumbers that have the same type, SimpleOperator.

It is important to understand that calling choosePlusMinus(true) does not execute the returned function and in fact only returns the reference to addTwoNumbers. We save this reference in chosenOperator. The chosenOperator variable becomes the following:

func addTwoNumbers(a: Double, b: Double) -> Double { return a + b } 

When we call chosenOperator(3.5, 5.5), we pass these two numbers to the addTwoNumbers function and execute it.

The capability to define function types makes functions first-class citizens in Swift. Function types are used for first-class and higher-order functions that are going to be explained in upcoming sections.