[concept]什么是闭包

Lambda Expressions@The Java™ Tutorials
https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html


Java 8里面正式推出了Lambda表达式。Lambda出现的背景,
One issue with anonymous classes is that if the implementation of your anonymous class is very simple, such as an interface that contains only one method, then the syntax of anonymous classes may seem unwieldy and unclear. In these cases, you're usually trying to pass functionality as an argument to another method, such as what action should be taken when someone clicks a button. Lambda expressions enable you to do this, to treat functionality as method argument, or code as data.

聚合操作
The operations filter, map, and forEach are aggregate operations. Aggregate operations process elements from a stream, not directly from a collection (which is the reason why the first method invoked in this example is stream).


A stream is a sequence of elements. Unlike a collection, it is not a data structure that stores elements. Instead, a stream carries values from a source, such as collection, through a pipeline.

通道
A pipeline is a sequence of stream operations, which in this example is filter- map-forEach.

一个例子,连起来使用,效率非常好,和脚本语言一致了。
In addition, aggregate operations typically accept lambda expressions as parameters, enabling you to customize how they behave.
roster
    .stream()
    .filter(
        p -> p.getGender() == Person.Sex.MALE
            && p.getAge() >= 18
            && p.getAge() <= 25)
    .map(p -> p.getEmailAddress())
    .forEach(email -> System.out.println(email));

lambda表达式语法
A comma-separated list of formal parameters enclosed in parentheses.
The arrow token
A body, which consists of a single expression or a statement block. 



Groovy closure
http://groovy.codehaus.org/Closures


定义
A Groovy Closure is like a "code block" or a method pointer. It is a piece of code that is defined and then executed at a later point. It has some special properties like implicit variables, support for currying and support for free variables

语法
A closure definition follows this syntax: { [closureArguments->] statements }

样例
def list = ['a','b','c','d']
def newList = []
def clos = { it.toUpperCase() }
list.collect( newList, clos )
assert newList == ["A", "B", "C", "D"]

JavaScript closure
http://jibbering.com/faq/notes/closures/
http://www.javascriptkit.com/javatutors/closures.shtml


定义
A "closure" is an expression (typically a function) that can have free variables together with an environment that binds those variables (that "closes" the expression).
a closure is the local variables for a function - kept alive after the function has returned, or
a closure is a stack-frame which is not deallocated when the function returns. (as if a 'stack-frame' were malloc'ed instead of being on the stack!)







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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值