- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
func performMathAverage (mathFunc: String) -> ([Int]) -> Double {
switch mathFunc {
case "mean":
return mean
case "median":
return median
default:
return mode
}
}
我从一本快速学习书中得到了这个例子,它谈到了返回函数类型的主题,这只是整个程序的一部分,我不想全部复制和粘贴。我的困惑是这本书说:
"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 ."
主要问题是:“为什么我们不调用方法?”我们返回对它的引用是什么意思??
引用是什么意思?我只是对这部分感到困惑。
最佳答案
您将您的主要问题表述为:
“为什么我们不调用方法?”我们返回对它的引用是什么意思??
一开始理解起来有点棘手,但它的意思是我们不想要函数的结果,我们想要函数本身。
有时像这样的事情使用类型别名更容易理解:
从 [Int] -> Int
开始,我们所说的是“一个接受 Int
数组并返回单个 整数
"
为了清楚起见,让我们创建一个类型别名:
typealias AverageFunction = [Int] -> Int
现在我们的函数(来自您的示例)如下所示:
func performMathAverage(mathFunc: String) -> AverageFunction {
不过,这里的命名约定很困惑,因为我们没有执行任何操作,而是让我们这样调用它:
func getAverageFunctionWithIdentifier(identifier: String) -> AverageFunction {
现在很明显,这个方法的功能就像一个工厂,它根据我们提供的标识符向我们返回一个平均函数。现在让我们看看实现:
func getAverageFunctionWithIdentifier(identifier: String) -> AverageFunction {
switch identifier {
case "mean":
return mean
case "median":
return median
default:
return mode
}
}
现在,我们在标识符上运行一个开关以找到相应的函数。同样,我们不调用该函数是因为我们不想要该值,我们想要该函数本身。让我们看看如何调用它:
let averageFunction = getAverageFunctionWithIdentifier("mean")
现在,averageFunction
是对 mean 函数的引用,这意味着我们可以使用它来获取整数数组的平均值:
let mean = averageFunction([1,2,3,4,5])
但是如果我们想使用不同类型的平均数,比如中位数呢?除了标识符,我们不必更改任何内容:
let averageFunction = getAverageFunctionWithIdentifier("median")
let median = averageFunction([1,2,3,4,5])
这个例子非常人为,但这样做的好处是通过将函数抽象为它的类型(在本例中 [Int] -> Int
,我们可以使用任何符合该类型可互换。
这就是函数式编程!
关于swift - 我无法弄清楚这个 switch 语句是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31991812/
我一直在试图理解人们一直在使用的这个网格系统。有时让我觉得自己很蠢。 我了解如果您使用无边距的 12 网格系统。第 12 列将是 100%,而第 1 列将约为 8.33333%。 我一直在看一些网格系
我们被分配了一个用于系统编程的 ASCII 压缩项目,但我在代码中的某一特定行上遇到了困难。 我问了question关于压缩,在处理完纸上示例文件的前十几个字母后,我将数组代码调整到了我的程序中。在
我正在使用 Appcelerator 框架编写应用程序,但偶尔会发生崩溃。我正在尝试找出导致崩溃的原因,因此我决定查看 iOS 模拟器崩溃报告。当然,这对我来说都是希腊语,但我希望得到一些指导,了解其
有人可以给我一些指导或指导我阅读有关 C++ set 对象的优秀教程吗? 我有一段这样的简单代码: #include using namespace std; int main() { ch
老实说,我不知道我的问题是否有解决方案,但我想在 Swift 中捕捉上下文切换发生的时间。 我正在想象一个需要很长时间才能完成的功能,例如远程服务器上的写操作,我在想是否有办法了解何时(至少在哪一行)
我正在使用 Yii2 并且一直在阅读 theming和 theme inheritance ;但是有一些问题: 考虑以下示例: 'view' => [ 'theme' => [
我尝试使用 AJAX 发布,因为我不想使用提交按钮并在每次单击它时重新加载页面。我正在使用此代码进行 ajax: Ajax loading error, please try again.").sho
我正在尝试找出将在 NodeJS 应用程序中使用的 MongoDB 模型的理想设计。该应用程序的设置类似于调查,某些步骤会根据之前的选择提供选项。这是选择和可能性的示例。 第 1 级:图案类型:纯色、
我有一个 API/Express 路由器: router.post("/signup", async function (req, res) { try { var user
我注意到 JFileChooser 隐藏了 Windows 系统文件。 hiberfil.sys、pagefile.sys、$Recycle.Bin 等文件、一些无法打开的快捷方式文件夹等... 我可
这是我第一次使用 Django,到目前为止,我对这个框架的工作方式印象深刻。我目前正在开发我的第一个应用程序,并正在处理数据库内容,但是,我在弄清楚如何在不运行原始查询的情况下进行内部联接时遇到问题。
我在自动调整蒙版大小方面遇到了一些问题。这是交易:我正在使用最近发布的 TwUI ,它从 UIKit 中获取了很多,但它在 Mac 上。这就是我为 iOS 和 Mac 标记的原因。因此,我创建了一个底
好吧,这是一个很长的,打起精神来! :) 最近我尝试在启动期间启动一个用 bash 编写的看门狗脚本。所以我在 rc.local 中添加了一行,其中包含以下内容: su someuser -c "/h
我在我的机器上安装了多个版本的 Windows 软件开发工具包,有趣的是,我的机器上已经安装了一个 Visual studio Installer工具的版本低于近一年前安装的版本: Windows S
widget('zii.widgets.CMenu', array( 'items'=>array( array('label'=>'Home', '
我是一名优秀的程序员,十分优秀!