- Swift Functional Programming(Second Edition)
- Dr. Fatih Nayebi
- 392字
- 2021-07-02 23:54:32
Higher-order functions
As we have seen in the Defining and using function parameters and Function types sections of this chapter, functions can accept functions as parameters in Swift. Functions that can accept other functions as parameters are called higher-order functions. This concept, along with first-class functions, empowers FP and function decomposition.
As this topic is essential in FP, we will go through another simple example.
Suppose that we need to develop two functions that add and subtract two Int values as follows:
func subtractTwoValues(a: Int, b: Int) -> Int {
return a - b
}
func addTwoValues(a: Int, b: Int) -> Int {
return a + b
}
Also, we need to develop functions to calculate the square and triple of two Int values as follows:
func square(a: Int) -> Int {
return a * a
}
func triple(a: Int) -> Int {
return a * a * a // or return square(a) * a
}
Suppose we need another function that subtracts the two squared values:
func subtractTwoSquaredValues(a: Int, b: Int) -> Int {
return (a * a) - (b * b)
}
In case we needed to add two squared values:
func addTwoSquaredValues(a: Int, b: Int) -> Int {
return (a * a) + (b * b)
}
Let's say that we need another function that triples a value and multiplies it with another tripled value:
func multiplyTwoTripledValues(a: Int, b: Int) -> Int {
return (a * a * a) * (b * b * b)
}
This way, we had to write a lot of redundant and inflexible functions. Using higher-order functions, we could write a flexible function as follows:
typealias AddSubtractOperator = (Int, Int) -> Int
typealias SquareTripleOperator = (Int) -> Int
func calculate(a: Int,
b: Int,
funcA: AddSubtractOperator,
funcB: SquareTripleOperator) -> Int {
return funcA(funcB(a), funcB(b))
}
This higher-order function takes two other functions as parameters and uses them. We can call it for different scenarios as follows:
print("The result of adding two squared values is: \(calculate(a: 2, b: 2, funcA: addTwoValues, funcB: square))")
// prints "The result of adding two squared value is: 8"
print("The result of subtracting two tripled value is: \(calculate(a: 3, b: 2, funcA: subtractTwoValues, funcB: triple))")
// prints "The result of adding two tripled value is: 19"
This simple example presented the utility of higher-order functions in function composition and subsequently in program modularity.
- 數(shù)據(jù)要素安全流通
- 數(shù)據(jù)庫基礎(chǔ)教程(SQL Server平臺)
- MongoDB管理與開發(fā)精要
- 數(shù)據(jù)庫開發(fā)實踐案例
- 云計算與大數(shù)據(jù)應(yīng)用
- 大話Oracle Grid:云時代的RAC
- Python金融數(shù)據(jù)分析(原書第2版)
- 數(shù)據(jù)庫應(yīng)用系統(tǒng)開發(fā)實例
- 貫通SQL Server 2008數(shù)據(jù)庫系統(tǒng)開發(fā)
- Unreal Engine Virtual Reality Quick Start Guide
- 菜鳥學(xué)SPSS數(shù)據(jù)分析
- 企業(yè)級大數(shù)據(jù)項目實戰(zhàn):用戶搜索行為分析系統(tǒng)從0到1
- 數(shù)據(jù)應(yīng)用工程:方法論與實踐
- Learning Game AI Programming with Lua
- R語言醫(yī)學(xué)多元統(tǒng)計分析