- Swift Functional Programming(Second Edition)
- Dr. Fatih Nayebi
- 624字
- 2021-07-02 23:54:32
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.
- MySQL高可用解決方案:從主從復制到InnoDB Cluster架構
- 大數據技術基礎
- SQL Server 2012數據庫技術與應用(微課版)
- R數據科學實戰:工具詳解與案例分析(鮮讀版)
- Python數據分析:基于Plotly的動態可視化繪圖
- Python金融實戰
- Oracle 12c云數據庫備份與恢復技術
- 大數據治理與安全:從理論到開源實踐
- 視覺大數據智能分析算法實戰
- 智慧的云計算
- Power BI智能數據分析與可視化從入門到精通
- 聯動Oracle:設計思想、架構實現與AWR報告
- Mastering ROS for Robotics Programming(Second Edition)
- SQL Server 2008寶典(第2版)
- Deep Learning with R for Beginners