Swift在闭包和第一类函数之间的任何区别? [英] Swift any difference between Closures and First-Class Functions?

查看:131
本文介绍了Swift在闭包和第一类函数之间的任何区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swift文档中,Apple说:

In the Swift documentation Apple says this:


闭包是自包含的功能块,可以通过
并在您的代码中使用。 Swift中的闭包类似于C和Objective-C中的块
,以及其他编程语言中的lambdas。

Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages.

思想是一流功能的定义

他们还说:


闭包可以捕获和存储对任何常量和
变量的引用定义。这被称为
关闭这些常量和变量。 Swift处理所有的
内存管理,为你捕获。

Closures can capture and store references to any constants and variables from the context in which they are defined. This is known as closing over those constants and variables. Swift handles all of the memory management of capturing for you.

我认为这是闭包的定义,定义是一级函数,但苹果似乎把它们放在一起,并称之为闭包。

I thought this was the definittion of closures while the other defitintion was for first-class functions, but Apple seems the put them together and call it closure.

我误解了什么吗?或者是Apple调用闭包和第一类函数闭包?

Have I misunderstood something? or are Apple calling closures and first-class functions closures?

我写了这个示例代码,只是想知道我是否在书面意见中? / p>

I've written this example code, and just wanna know if I'm right in the written comments?

// 'a' takes a first class function, which makes 'a' a higher order function
func a(ch: () -> Void){
    print("Something")
    ch()                // 'ch' is a first class function
    print("Ended")
}

func closureFunc(){
    var num = 2
    a({
        // access to 'num' is possible by closures
        num = num*2
        print(num)
    })
}

closureFunc()


推荐答案

这些概念是正交的。他们不直接相关;它们是关于Swift中函数的两个事实。

These notions are orthogonal. They are not directly related; they are two facts about functions in Swift.


  • 函数 first-class 。这意味着它们可以传递 - 作为变量分配,作为参数传递到函数参数,并作为结果传递出函数。

  • Functions are first-class. This means they can be passed around — assigned as variables, passed into function parameters as arguments, and passed out of functions as results.

函数闭包。这意味着,在定义点,它们捕获了内部的环境,但在之外声明了

Functions are closures. This means that, at the point of definition, they capture the environment referred to inside the function body but declared outside the function body.

下面是一个例子(来自操场):

Here is an example (from a playground):

func multiplierMaker(i:Int) -> (Int) -> (Int) {
    func multiplier(ii:Int) -> (Int) {
        return ii*i
    }
    return multiplier
}
let g = multiplierMaker(10)
g(2) // 20

考虑函数 multiplier


  • 事实上乘法器可以作为函数 multiplierMaker ,并分配给 g ,并且它有一个定义良好的类型 > (Int),是因为函数是一级

  • The fact that multiplier can be returned as the result of the function multiplierMaker, and assigned to g, and that it has a well-defined type (Int) -> (Int), is because functions are first-class.

被传递到 multiplierMaker 中,所以乘法器函数将其参数乘以10,即使分配给 g 后调用,是因为函数是闭包

The fact that, when 10 is passed into multiplierMaker, the resulting multiplier function multiplies its parameter by 10, even when assigned to g and called later, is because functions are closures.

(请注意,这与匿名函数无关。所有的答案或语句都会导致你认为闭包与匿名函数有关,在这个例子中没有匿名函数。匿名函数一个闭包,但只是因为所有函数都是闭包。)

(Notice that this has nothing to do with anonymous functions. All answers or statements leading you to believe that closures have to do with anonymous functions are wrong. There are no anonymous functions in this example. An anonymous function is a closure, but only because all functions are closures.)

这篇关于Swift在闭包和第一类函数之间的任何区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆