默认情况下,它们是由值传递的,意味着如果要更新传递到闭包函数定义内的值,则不更新外部值。
By default, they are passed by value, meaning that if we would update the value passed within the closure function definition, it would not update the outside value.
例如,若要处理一项事务,采用闭包后,您就能确保事务代码总能由适当的函数调用界定。
For example, if you have a transaction, you can make sure that your code always brackets transactional code with the appropriate function calls.
像许多动态语言一样,这些函数在它们被定义的范围内创建了词法闭包。
These functions create a lexical closure on the scope in which they were defined like blocks in many dynamic languages.
在本文中,我将继续讨论这些概念,讲解一级函数、优化和闭包。
In this article, I'll continue this tour of concepts by talking about first-class functions, optimizations, and closures.
如果使用过高阶函数或闭包,那么可能更容易理解清单10中的代码。
If you've ever used closures or higher-order functions, you might have a better understanding of the code in Listing 10.
后续内容还会对闭包与高阶函数做一些有趣的对比。
I'll also circle back to some interesting comparisons between closures and higher-order functions in a later installment.
这些由列表构建的简单指令构成了递归、高阶函数,甚至是闭包和continuation之类高级抽象的基础。
These simple list-building instructions form the foundation of recursions, higher-order functions, and even higher-order abstractions like closures and continuations.
从外部环境中导入的变量是在闭包函数定义的use子句中指定的。
Variables to be imported from the outside environment are specified in the use clause of the closure function definition.
表达式闭包是定义简单函数的一种便捷方式,用来模仿lambda符号。
Expression closures are a shorthand method of defining simple functions that mimics lambda notation.
一流的方法和函数(闭包)
同样,可以用writeapi函数来探测对等套接字的闭包。
The closure of a peer socket can also be detected with the write API function.
如清单1中所示,Example对象的成员函数被定义为闭包。
As we see in Listing 1, the member functions of the Example object are defined as closures.
这种情况下返回的函数对象就是一个闭包,其中封装了一些日志代码和最初的函数调用所需要的数据。
In this case the returned function object is a closure which encapsulates some logging code and the data needed to call the original function.
闭包是函数语言的共有特征之一。
Closures are one of the common features of functional languages.
一个闭包封闭了这些引用,所以函数调用可以安全的引用这些变量,即使它们已经超出了函数的作用域。
A closure "closes around" these references, so the function invocation can safely refer to the variables even when the variables have gone out of scope!
把闭包应用于函数模式是令人兴奋的,一旦这么做了之后,再把它们应用于面向对象设计模式,就不是什么大事情了。
Applying closures to functional patterns is exciting, and once you've done that, it's not too big a stretch to apply them to object-oriented design patterns.
我们先从一个机制开始,用这个机制可以表示包含计算模式的闭包,计算模式是一个来自函数式编程的概念。
We'll start with a mechanism by which you can express closures that embody a pattern of computation, which is a concept from functional programming.
以上演示了为什么闭包体现了函数思想:让运行时管理状态。
This illustrates why the use of closures exemplifies functional thinking: allow the runtime to manage state.
这个闭合的作用域是将闭包同调用函数和定义它的代码联系起来的纽带。
This closed scope is the communication that ties the closure to the calling function and the code that defines it.
我们还在闭包内获取狗的颜色和名字,传递到构造函数中并存储到对象中。
We also grab the color and name of the dog, passed in the constructor and stored in the object, within the closure.
好奇的读者会想是什么使仿函数不同于匿名函数或闭包呢?
Curious readers will be wondering what makes a functor different from an anonymous function or closure.
闭包本质上是一个函数和其引用的变量的统一定义。
Closures are functions that reference variables from the scope enclosing the function definition.
当内部函数获得和使用其外部函数的变量时,就称其为一个闭包。
When an inner function gains and USES access to its outer function's variables it is known as a closure.
在这种情况下使用闭包与在类外部使用闭包实现的目的相同:包含在小范围内绑定的特定函数。
Using closures serves the same purpose in this situation as it would outside of a class: to contain a specific function to be bound within a small scope.
闭包是具有闭合作用域的匿名函数。
正如我们所见,反射API能够通过现有函数和方法动态创建闭包,从而为闭包提供强大的支持。
The reflection API has great support for closures, as we've now seen, in the form of being able to create them from existing functions and methods dynamically.
它在此上下文中用作宏,通过闭包调用函数方法将在定义它的上下文中执行函数调用。
It ACTS like a macro in this context, where calling the method of function via the closure makes the function call in the context of where it's defined.
如果碰巧该闭包函数跟某个事件绑定,那么就必须要解绑定。
If you happened to attach the closure to an event, then you would have to detach it from that event.
闭包函数和lambda函数绝对不是新出现的概念;它们均来自函数编程领域。
The concepts of closures and lambda functions are definitely not new ones; they both come from the functional programming world.
闭包函数和lambda函数绝对不是新出现的概念;它们均来自函数编程领域。
The concepts of closures and lambda functions are definitely not new ones; they both come from the functional programming world.
应用推荐