Closures 闭包

        在编程语言中,闭包(词法闭包or函数闭包)是一种在支持first-class functions语言中,实现词法作用域的名字绑定的技术。操作实现上,闭包指将函数与其环境一起保存起来的记录:当闭包被创建时,映射关联每个函数的非局部(本地)变量 到 它的值或者名字已被限制的存储位置。闭包不同于普通的函数,允许函数访问这些通过闭包引用而捕获的变量,甚至在变量作用域之外调用函数。 

        例子: 以下程序片断定义了高阶函数 startAt ,它包括x参数和一个嵌套的函数incrementBy。嵌套的函数访问了x,因为incrementBy在x的词义作用域中,即使x不在incrementBy本地。函数 startAt返回了容纳了函数incrementBy的闭包,incrementBy将y的值与x的值相加,也从startAt的这次调用返回了x变量的引用,于是incrementBy一旦被调用就知道去哪里找到x:

function startAt(x)
   function incrementBy(y)
       return x + y
   return incrementBy

variable closure1 = startAt(1)
variable closure2 = startAt(5)
       注意,因为startAt返回的是函数,变量closure1和 closure2是函数类型的。closure1(3)调用将返回4,而closure2(3)将返回8。因为closure1和closure2引用同一个函数incrementBy,关联的环境不同。所以在两次调用中,调用的闭包绑定名字x到值不同的不同变量。最终函数得到不同的结果。



In programming languagesclosures (also lexical closures or function closures) are a technique for implementing lexically scoped name binding in languages with first-class functionsOperationally, a closure is a record storing a function together with an environment: a mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope) with the value or storage location to which the name was bound when the closure was created. A closure—unlike a plain function—allows the function to access those captured variables through the closure's reference to them, even when the function is invoked outside their scope.


Example. The following program fragment defines a higher-order function startAt with a parameter x and a nested function incrementBy. The nested function incrementBy has access to x, because incrementBy is in the lexical scope of x, even though x is not local to incrementBy. The function startAt returns a closure containing the function incrementBy, which adds the y value to the x value, and a reference to the variable x from this invocation of startAt, so incrementBy will know where to find it once invoked:

function startAt(x)
   function incrementBy(y)
       return x + y
   return incrementBy

variable closure1 = startAt(1)
variable closure2 = startAt(5)

Note that, as startAt returns a function, the variables closure1 and closure2 are of function type. Invoking closure1(3) will return 4, while invoking closure2(3) will return 8. While closure1 and closure2 refer to the same function incrementBy, the associated environments differ, and invoking the closures will bind the name x to two distinct variables with different values in the two invocations, thus evaluating the function to different results.


原文链接: https://en.wikipedia.org/wiki/Closure_(computer_programming)


以下内容解释了:first-class functions和higher-order function


 1.     In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. Specifically, this means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures. Some programming language theorists require support for anonymous functions (function literals) as well. In languages with first-class functions, thenames of functions do not have any special status; they are treated like ordinary variables with a function type. The term was coined by Christopher Strachey in the context of "functions as first-class citizens" in the mid-1960s.


2.  A function type depends on the type of the parameters and the result type of the function。

In mathematics and computer science, a higher-order function (also functionalfunctional form or functor) is a function that does at least one of the following:

  • takes one or more functions as arguments 
  • returns a function as its result.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值