- Swift Functional Programming(Second Edition)
- Dr. Fatih Nayebi
- 457字
- 2021-07-02 23:54:33
Closure syntax
A general closure syntax is as follows:
{ (parameters) -> ReturnType in // body of closure }
A closure definition starts with {, then we define the closure type, and finally we use the in keyword to separate the closure definition from its implementation.
After the in keyword, we write the body of the closure and finish our closure by closing }.
Closures can be used to define variables or stored as variables. The following closure defines a variable of a type closure that accepts Int and returns Int:
let closureName: (Int) -> (Int) = { _ in 10 }
Closures can be stored as optional variables. The following closure defines a variable of a type closure that accepts Int and returns Optional Int
var closureName: (Int) -> (Int)?
Closures can be defined as typealiases. The following example presents typealias of a closure that has two Int parameters and returns Int.
typealias closureType = (Int, Int) -> (Int)
The same typealias could be used for a function type definition as functions are named closures in Swift.
Closures can be used as an argument to a function call. For instance, the following example presents a function that is called with a closure that receives Int and returns Int:
func aFunc(closure: (Int) -> Int) -> Int {
// Statements, for example:
return closure(5)
}
let result = aFunc(closure: {
number in
// Statements, for example:
return number * 3
})
print(result) // prints 15
Closures can be used as function parameters. The following example shows an array sort method that receives a closure:
var anArray = [1, 2, 5, 3, 6, 4]
anArray.sort(by: {
(param1: Int, param2: Int) -> Bool in
return param1 < param2
})
This syntax can be simplified with implied types as the Swift compiler has the ability to infer the types for parameters from the context as follows:
anArray.sort(by: {
(param1, param2) -> Bool in
return param1 < param2
})
The syntax can be further simplified with implied return types using the Swift type inference:
anArray.sort(by: {
(param1, param2) in
return param1 < param2
})
Swift enables us to eliminate the parameter name and open and close parentheses if we need to pass the closure as the last parameter of a function, in other words, if our closure is a trailing closure:
anArray.sort {
(param1, param2) in
return param1 < param2
}
Also, Swift provides a shorthand argument notation that can be used instead of using arguments:
anArray.sort {
return $0 < $1
}
We can simplify this syntax even further by eliminating the return keyword as we have only one line of expression, as follows:
anArray.sort { $0 < $1 }
Using the Swift type inference, we were able to shorten the closure syntax drastically. Shorter syntax does not mean a simpler code to read and understand. It is recommended to find the optimum syntax that suits you and your team and adapt it consistently.
- 漫話大數(shù)據(jù)
- 從零開(kāi)始學(xué)Hadoop大數(shù)據(jù)分析(視頻教學(xué)版)
- 數(shù)據(jù)庫(kù)基礎(chǔ)與應(yīng)用:Access 2010
- 文本挖掘:基于R語(yǔ)言的整潔工具
- 深入淺出MySQL:數(shù)據(jù)庫(kù)開(kāi)發(fā)、優(yōu)化與管理維護(hù)(第2版)
- OracleDBA實(shí)戰(zhàn)攻略:運(yùn)維管理、診斷優(yōu)化、高可用與最佳實(shí)踐
- SQL應(yīng)用及誤區(qū)分析
- 探索新型智庫(kù)發(fā)展之路:藍(lán)迪國(guó)際智庫(kù)報(bào)告·2015(上冊(cè))
- 數(shù)據(jù)庫(kù)應(yīng)用系統(tǒng)開(kāi)發(fā)實(shí)例
- 新手學(xué)會(huì)計(jì)(2013-2014實(shí)戰(zhàn)升級(jí)版)
- 中文版Access 2007實(shí)例與操作
- 數(shù)據(jù)庫(kù)查詢優(yōu)化器的藝術(shù):原理解析與SQL性能優(yōu)化
- MySQL技術(shù)內(nèi)幕:InnoDB存儲(chǔ)引擎
- 利用Python進(jìn)行數(shù)據(jù)分析(原書(shū)第2版)
- 數(shù)據(jù)挖掘與數(shù)據(jù)化運(yùn)營(yíng)實(shí)戰(zhàn):思路、方法、技巧與應(yīng)用