第二个参数是一个函数闭包。
在这个案例中您使用一个匿名内联函数,也称为一个闭包。
In this case, you used an anonymous inline function, also known as a closure.
对于这个函数,我们再次使用了一个简短的闭包注释。
For the function, we are once again using the shorthand closure notation.
默认情况下,它们是由值传递的,意味着如果要更新传递到闭包函数定义内的值,则不更新外部值。
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.
如果没有指定,函数将在定义它的闭包中运行。
A function will run in the closure that defines it unless otherwise specified.
我们也可以拥有直接返回闭包的函数,如清单6 所示。
We can also have functions that directly return closures, as we see in Listing 6.
方法或函数可通过传入参数与闭包通信。
A method or function can communicate with a closure by passing in arguments.
要了解闭包,首先需要了解匿名函数。
To understand closures, one first has to understand anonymous functions.
在本文中,我将继续讨论这些概念,讲解一级函数、优化和闭包。
In this article, I'll continue this tour of concepts by talking about first-class functions, optimizations, and closures.
对函数使用断言通常很简单,而且最好使用闭包定义。
Predicate functions are often very simple and better off being defined using a closure.
如果使用过高阶函数或闭包,那么可能更容易理解清单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.
因此出现了闭包并使lambda函数得到增强。
This is where closures step in and take the lambda functions to the next level.
这种情况下返回的函数对象就是一个闭包,其中封装了一些日志代码和最初的函数调用所需要的数据。
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.
以上演示了为什么闭包体现了函数思想:让运行时管理状态。
This illustrates why the use of closures exemplifies functional thinking: allow the runtime to manage state.
它们还可以像普通函数一样内省到闭包中。
They can also introspect into a closure just like a normal function can.
这个闭合的作用域是将闭包同调用函数和定义它的代码联系起来的纽带。
This closed scope is the communication that ties the closure to the calling function and the code that defines it.
好奇的读者会想是什么使仿函数不同于匿名函数或闭包呢?
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.
闭包是具有闭合作用域的匿名函数。
应用推荐