我无法弄清楚这个 switch 语句是如何工作的?
I can't figure out how this switch statement is working?
func performMathAverage (mathFunc: String) -> ([Int]) -> Double {
switch mathFunc {
case "mean":
return mean
case "median":
return median
default:
return mode
}
}
我从一本 swift 学习书中得到了这个例子,它谈到了返回函数类型的主题,这只是整个程序的一部分,我不想全部复制和粘贴。我的困惑是这本书说:
"Notice in performMathAverage , inside the switch cases, we return
either mean , median , or mode , and not mean() , median() , or mode()
. This is because we are not calling the methods, rather we are
returning a reference to it, much like function pointers in C. When
the function is actually called to get a value you add the parentheses
suffixed to the function name. Notice, too, that any of the average
functions could be called independently without the use of the
performMathAverage function. This is because mean , median , and mode
are called global functions ."
主要问题是:"Why are we not calling the methods?"
他们是什么意思我们正在返回对它的引用??
引用是什么意思?我只是对这部分感到困惑。
这与 functional programming aspects of swift. Here functions are treated like first class citizens 有关,意思是您可以将它们视为变量。
为什么我们不调用方法?
您没有调用方法,因为您没有要应用的参数。该函数的要点是确定 您要使用哪个 函数。当然函数的名字很糟糕,并不能准确地表示函数的作用。它应该更像func determineMathFuncToUse
,然后你可以像
那样使用它
var myFunc = determineMathFuncToUse("median")
// Now, you would be able to use myFunc just like you would use median
// e.g. myFunc(some_array) == median(some_array)
这很容易理解。在 Swift 中,您可以存储对函数的引用(在 Objective-C 中最接近的是对块的引用)。
func performMathAverage (mathFunc: String) -> ([Int]) -> Double
这是 return 类型为:
的函数
([Int]) -> Double
如您所见,此函数的 return 类型是一个接受 Int
和 returns Double
.
数组的函数
并且在代码中您看到它 return 是三个函数之一:mean
、mode
和 median
。这些函数中的每一个都接受 Int
和 returns Double
.
的数组
由于以下代码:
let meanFunc = performMathAverage("mean")
let mean = meanFunc(someIntArray)
等同于:
let mean = mean(someIntArray)
希望对您有所帮助。
函数未在代码中执行的原因是因为此示例说明了如何存储对函数的引用。
可能很难理解您为什么要在这种特殊情况下这样做,但是,嘿,打印 "Hello world" 似乎也毫无意义 :)
您指的是教程中的示例,因此它们过于简单化也就不足为奇了。但是,请相信我,在现实世界中有很多情况下存储对函数的引用非常非常有用!
一个明显的例子是当您想存储对某个完成处理程序的引用时,您希望在某个冗长的操作结束时执行该处理程序。根据您启动此操作的上下文,这可能会有所不同。
为了回答您的问题,您实际上是在返回函数本身,而不是调用该函数的结果。在这种情况下,它允许您选择一个函数(使用 switch 语句)并稍后对其进行评估。一个有用的思考方式是函数也是一种变量,您可以传递它们也可以计算它们。
作为一般风格的事物,最好以 break
结束每个 case
。这里没有区别,因为 return
也会结束函数的执行,但是没有中断,所有 在 之后的代码都会执行正确的 case
,而不是只是代码 在 中正确的 case
。 运行 到另一个 case
语句本身不会跳出 switch 语句。
Why are we not calling the methods?
估计稍后会调用该方法。 switch语句的目的是为了return一个以后可以使用的功能
What do they mean we are returning a reference to it? ... What do they mean by reference?
"reference" 语言有点令人困惑 - 函数是引用类型,但这对于正在发生的事情并不是特别重要。您可以将其视为 return 一个函数。
底线是 swift 中的函数可以像任何其他类型一样使用 - 它们可以存储在变量或常量中,它们可以作为参数传递给函数,并且它们可以是 return从函数中编辑。
在这种情况下,您有一个旨在 return 函数的函数。如果你想获得平均值,你传递字符串 "mean" 并且函数将 return 一个新函数,当你调用它时将获得平均值。
您将主要问题表述为:
"Why are we not calling the methods?" 我们返回对它的引用是什么意思??
一开始理解起来有点棘手,但它的意思是我们不想要函数的结果,我们想要函数本身。
有时像这样的事情使用类型别名更容易理解:
从 [Int] -> Int
开始,我们要说的是 "a function that takes an array of Int
s and returns a single Int
"
为了清楚起见,让我们创建一个类型别名:
typealias AverageFunction = [Int] -> Int
现在我们的函数(根据您的示例)如下所示:
func performMathAverage(mathFunc: String) -> AverageFunction {
虽然这里的命名约定很混乱,因为我们没有执行任何操作,但我们这样称呼它:
func getAverageFunctionWithIdentifier(identifier: String) -> AverageFunction {
现在很明显,这个方法的功能就像一个工厂,returns 我们根据我们提供的标识符使用一个平均函数。现在让我们看看实现:
func getAverageFunctionWithIdentifier(identifier: String) -> AverageFunction {
switch identifier {
case "mean":
return mean
case "median":
return median
default:
return mode
}
}
所以现在,我们运行打开标识符以找到相应的函数。同样,我们不调用该函数是因为我们不想要该值,我们想要该函数本身。让我们看看如何称呼它:
let averageFunction = getAverageFunctionWithIdentifier("mean")
现在,averageFunction
是对均值函数的 引用,这意味着我们可以使用它来获取整数数组的均值:
let mean = averageFunction([1,2,3,4,5])
但是如果我们想使用不同类型的平均数,比如中位数怎么办?除了标识符之外,我们不需要更改任何内容:
let averageFunction = getAverageFunctionWithIdentifier("median")
let median = averageFunction([1,2,3,4,5])
这个例子非常人为设计,但这样做的好处是通过将函数抽象为它的类型(在本例中 [Int] -> Int
,我们可以互换地使用符合该类型的任何函数。
这就是函数式编程!
func performMathAverage (mathFunc: String) -> ([Int]) -> Double {
switch mathFunc {
case "mean":
return mean
case "median":
return median
default:
return mode
}
}
我从一本 swift 学习书中得到了这个例子,它谈到了返回函数类型的主题,这只是整个程序的一部分,我不想全部复制和粘贴。我的困惑是这本书说:
"Notice in performMathAverage , inside the switch cases, we return either mean , median , or mode , and not mean() , median() , or mode() . This is because we are not calling the methods, rather we are returning a reference to it, much like function pointers in C. When the function is actually called to get a value you add the parentheses suffixed to the function name. Notice, too, that any of the average functions could be called independently without the use of the performMathAverage function. This is because mean , median , and mode are called global functions ."
主要问题是:"Why are we not calling the methods?" 他们是什么意思我们正在返回对它的引用??
引用是什么意思?我只是对这部分感到困惑。
这与 functional programming aspects of swift. Here functions are treated like first class citizens 有关,意思是您可以将它们视为变量。
为什么我们不调用方法?
您没有调用方法,因为您没有要应用的参数。该函数的要点是确定 您要使用哪个 函数。当然函数的名字很糟糕,并不能准确地表示函数的作用。它应该更像func determineMathFuncToUse
,然后你可以像
var myFunc = determineMathFuncToUse("median")
// Now, you would be able to use myFunc just like you would use median
// e.g. myFunc(some_array) == median(some_array)
这很容易理解。在 Swift 中,您可以存储对函数的引用(在 Objective-C 中最接近的是对块的引用)。
func performMathAverage (mathFunc: String) -> ([Int]) -> Double
这是 return 类型为:
的函数([Int]) -> Double
如您所见,此函数的 return 类型是一个接受 Int
和 returns Double
.
并且在代码中您看到它 return 是三个函数之一:mean
、mode
和 median
。这些函数中的每一个都接受 Int
和 returns Double
.
由于以下代码:
let meanFunc = performMathAverage("mean")
let mean = meanFunc(someIntArray)
等同于:
let mean = mean(someIntArray)
希望对您有所帮助。
函数未在代码中执行的原因是因为此示例说明了如何存储对函数的引用。
可能很难理解您为什么要在这种特殊情况下这样做,但是,嘿,打印 "Hello world" 似乎也毫无意义 :)
您指的是教程中的示例,因此它们过于简单化也就不足为奇了。但是,请相信我,在现实世界中有很多情况下存储对函数的引用非常非常有用!
一个明显的例子是当您想存储对某个完成处理程序的引用时,您希望在某个冗长的操作结束时执行该处理程序。根据您启动此操作的上下文,这可能会有所不同。
为了回答您的问题,您实际上是在返回函数本身,而不是调用该函数的结果。在这种情况下,它允许您选择一个函数(使用 switch 语句)并稍后对其进行评估。一个有用的思考方式是函数也是一种变量,您可以传递它们也可以计算它们。
作为一般风格的事物,最好以 break
结束每个 case
。这里没有区别,因为 return
也会结束函数的执行,但是没有中断,所有 在 之后的代码都会执行正确的 case
,而不是只是代码 在 中正确的 case
。 运行 到另一个 case
语句本身不会跳出 switch 语句。
Why are we not calling the methods?
估计稍后会调用该方法。 switch语句的目的是为了return一个以后可以使用的功能
What do they mean we are returning a reference to it? ... What do they mean by reference?
"reference" 语言有点令人困惑 - 函数是引用类型,但这对于正在发生的事情并不是特别重要。您可以将其视为 return 一个函数。
底线是 swift 中的函数可以像任何其他类型一样使用 - 它们可以存储在变量或常量中,它们可以作为参数传递给函数,并且它们可以是 return从函数中编辑。
在这种情况下,您有一个旨在 return 函数的函数。如果你想获得平均值,你传递字符串 "mean" 并且函数将 return 一个新函数,当你调用它时将获得平均值。
您将主要问题表述为:
"Why are we not calling the methods?" 我们返回对它的引用是什么意思??
一开始理解起来有点棘手,但它的意思是我们不想要函数的结果,我们想要函数本身。
有时像这样的事情使用类型别名更容易理解:
从 [Int] -> Int
开始,我们要说的是 "a function that takes an array of Int
s and returns a single Int
"
为了清楚起见,让我们创建一个类型别名:
typealias AverageFunction = [Int] -> Int
现在我们的函数(根据您的示例)如下所示:
func performMathAverage(mathFunc: String) -> AverageFunction {
虽然这里的命名约定很混乱,因为我们没有执行任何操作,但我们这样称呼它:
func getAverageFunctionWithIdentifier(identifier: String) -> AverageFunction {
现在很明显,这个方法的功能就像一个工厂,returns 我们根据我们提供的标识符使用一个平均函数。现在让我们看看实现:
func getAverageFunctionWithIdentifier(identifier: String) -> AverageFunction {
switch identifier {
case "mean":
return mean
case "median":
return median
default:
return mode
}
}
所以现在,我们运行打开标识符以找到相应的函数。同样,我们不调用该函数是因为我们不想要该值,我们想要该函数本身。让我们看看如何称呼它:
let averageFunction = getAverageFunctionWithIdentifier("mean")
现在,averageFunction
是对均值函数的 引用,这意味着我们可以使用它来获取整数数组的均值:
let mean = averageFunction([1,2,3,4,5])
但是如果我们想使用不同类型的平均数,比如中位数怎么办?除了标识符之外,我们不需要更改任何内容:
let averageFunction = getAverageFunctionWithIdentifier("median")
let median = averageFunction([1,2,3,4,5])
这个例子非常人为设计,但这样做的好处是通过将函数抽象为它的类型(在本例中 [Int] -> Int
,我们可以互换地使用符合该类型的任何函数。
这就是函数式编程!