Closures 闭包
2016-05-11 18:52
197 查看
在编程语言中,闭包(词法闭包or函数闭包)是一种在支持first-class functions语言中,实现词法作用域的名字绑定的技术。操作实现上,闭包指将函数与其环境一起保存起来的记录:当闭包被创建时,映射关联每个函数的非局部(本地)变量 到 它的值或者名字已被限制的存储位置。闭包不同于普通的函数,允许函数访问这些通过闭包引用而捕获的变量,甚至在变量作用域之外调用函数。
例子: 以下程序片断定义了高阶函数 startAt ,它包括x参数和一个嵌套的函数incrementBy。嵌套的函数访问了x,因为incrementBy在x的词义作用域中,即使x不在incrementBy本地。函数
startAt返回了容纳了函数incrementBy的闭包,incrementBy将y的值与x的值相加,也从startAt的这次调用返回了x变量的引用,于是incrementBy一旦被调用就知道去哪里找到x:
In programming
languages, closures (also lexical
closures or function
closures) are a technique for implementing lexically
scoped name
binding in languages with first-class
functions. Operationally,
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:
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 functional, functional
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.
例子: 以下程序片断定义了高阶函数 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
languages, closures (also lexical
closures or function
closures) are a technique for implementing lexically
scoped name
binding in languages with first-class
functions. Operationally,
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 functional, functional
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.
相关文章推荐
- springMVCdemo
- poj 2775 文件结构“图”(递归)
- WPF中Grid实现网格,表格样式通用类
- 堆的实现例程
- wins安装Java
- 位运算及其应用详解
- 一个关于if语句的小问题
- socket(TCP)
- nyoj 228 士兵杀敌(五)
- C++对象的内存分布和虚函数表
- 代码删除项目里面所有.svn和.git 文件
- 错误 1error LNK1207 PDB 格式不兼容;请删除并重新生成
- UIGesture
- HDOJ 1042 N!
- nefu26(求数的位数)
- 探索 Swift 中的 MVC-N 模式
- LNMP源码编译安装(centos7+nginx1.9+mysql5.6+php7)
- HDU4165 (卡特兰数列) Pills(简单期望DP)
- java home 配置
- n!后面有多少个0