15 使用lambdas和闭包
1 使用lambdas和闭包
1.1 定义闭包
闭包是一个代码块,代替了方法或类。
groovy中的闭包基于后边的构造方式:{list of parameters-> closure body}.其中,-> 之前的值为声明的闭包参数。
如果只有一个变量的时候,可以使用固有变量 it 。
如果没有返回值被定义,则返回-> 后边的值。it 的返回值的用法,参见下例子,
|
package closures class ClosuresTest { static main(args) { //返回input,使用固有的变量it def returnInput = {it}; assert "test" == returnInput("test"); //返回input,不使用固有的变量it def returnInput2 = {s->s}; assert "test" == returnInput2("test"); } } |
1.2 闭包中定义默认值
闭包中,也可以定义参数的默认值。
|
package closures class ClosuresDefaultValue { static main(args) { def multiply = {int a,int b=10 -> a*b; }; assert multiply(2) == 20; assert multiply(2,5) == 10 } } |
1.3 例子:each方法中使用闭包
在集合中使用闭包的例子,
|
package closures class ClosuresEach { static main(args) { List<Integer> list = [5,6,7,8]; println ("====自定义变量"); list.each {line -> print line+","; }; println ("\r\n====固有变量"); list.each ({ print it+","; }); println ("\r\n====计算从1到10的和"); def total = 0; (1..10).each { total += it; }; println total; } } |
1.4 例子:通过string的长度对list排序
|
package closures class ClosuresSort { static main(args) { def List strings = "this is a long sentence".split(); strings.sort{s1,s2 -> s1.size() <=> s2.size(); }; println strings; } } |
输出
|
[a, is, this, long, sentence] |
1.5 使用with方法
每个groovy对象都有一个with 方法,在该方法内,允许调用多个方法或属性,并将所设置的值或执行的方法都应用到该对象中。
|
package closures import java.util.List; class WithObj { String property1; String property2; List<String> list = []; def addElement(value) { list << value; }; def returnProperties() { "Property 1:$property1,Property 2:$property2"; } } |
|
package closures class ClosuresWithMethod { static main(args) { def sample = new WithObj(); def result = sample.with { property1="Input 1"; property2="This is cool"; addElement("Ubuntu"); addElement("Android"); addElement("Linux"); returnProperties(); }; println result; assert 3==sample.list.size(); assert "Input 1" == sample.property1; assert "This is cool" == sample.property2; assert "Linux" == sample.list[2]; def sb = new StringBuilder(); sb.with { append("this "); append("is "); append("appended"); }; println sb; } } |
输出:
|
Property 1:Input 1,Property 2:This is cool this is appended |
15 使用lambdas和闭包的更多相关文章
- 15 python 初学(闭包,函数装饰器)
这一部分很重要,一定要透彻理解.可参考大神博客: http://www.cnblogs.com/yuanchenqi/articles/5830025.html 闭包: 如果在一个内部函数里,对在外 ...
- Python入门笔记(22):Python函数(5):变量作用域与闭包
一.全局变量与局部变量 一个模块中,最高级别的变量有全局作用域. 全局变量一个特征就是:除非被删除,否则他们存活到脚本运行结束,且对于所有的函数都可访问. 当搜索一个标识符(也称变量.名字等),Pyt ...
- Groovy闭包
定义 闭包(Closure)是一种数据类型,它代表一段可执行的代码.它可以作为方法的参数,或者返回值,也可以独立运行,定义如下: def xxx = {parameters -> code} ...
- Swift5 语言指南(九) 闭包
闭包是自包含的功能块,可以在代码中传递和使用.Swift中的闭包类似于C和Objective-C中的块以及其他编程语言中的lambdas. 闭包可以从定义它们的上下文中捕获和存储对任何常量和变量的引用 ...
- 016.Python闭包函数以及locals和globals
一 闭包函数 内函数使用了外函数的局部变量,并且外函数把内函数返回出来的过程叫做闭包,这个内函数叫做闭包函数 1.1 闭包函数语法 def outer(): a = 5 def inner(): pr ...
- 我的游戏蜗牛web前端面试经历
蜗牛在江苏苏州地区应该算是比较大的互联网公司了,可以称得上中国游戏的鼻祖,之前一直很想进蜗牛,但作为一个应届毕业生却没有看到蜗牛发布任何关于招聘实习生的职位,无奈之下于是就毛遂自荐了,主动以邮件的形式 ...
- 自学Python二 Python中的屠龙刀(续)
函数 秉承着一切皆对象的理念,函数作为对象,可以为其赋值新的对象名,也可以作为参数传递给其他函数! 正常的诸如空函数,默认参数等等我们就不提了,在这里着重提一下默认参数里面的坑和lambda函数. 当 ...
- PHP5.3, PHP5.4, PHP5.5新特性
PHP 5.3中的新特性 1. 支持命名空间 (Namespace) 2. 支持延迟静态绑定(Late Static Binding) 3. 支持goto语句 4. 支持闭包.Lambda/Anony ...
- 前端面试题第一波,要offer的看过来~
一.HTML常见题目 01.Doctype作用?严格模式与混杂模式如何区分?它们有何意义? 02.HTML5为什么只需要写<!DOCTYPE HTML>? 03.行内元素有哪些?块级元素有 ...
随机推荐
- codeforces 的 Codeforces Round #273 (Div. 2) --C Table Decorations
C. Table Decorations time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CSU - 1803 —— 数学题
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1803 Description 给出正整数 n 和 m,统计满足以下条件的正整数对 ...
- Android加载/处理超大图片神器!SubsamplingScaleImageView(subsampling-scale-image-view)【系列1】
Android加载/处理超大图片神器!SubsamplingScaleImageView(subsampling-scale-image-view)[系列1] Android在加载或者处理超大巨型图片 ...
- linux应用之tomcat的安装及配置(centos)
CentOS 6.6下安装配置Tomcat环境 [日期:2015-08-25] 来源:Linux社区 作者:tae44 [字体:大 中 小] 实验系统:CentOS 6.6_x86_64 实验前 ...
- 算法实现c语言--01
打印九九乘法表 #include<stdio.h> #include<stdlib.h> int main() { , j = ; ; i <= ; i++) { ; j ...
- epoll的一个使用例子
使用到主要函数有: #include <sys/epoll.h> int epoll_create(int size); int epoll_create1(int flags); int ...
- disablescroll
页面的设置 disablescroll:true(需要配合设置 enablePullDownRefresh:false ) 可以实现页面上下不能滑动 另一种实现方法: 设置页面的根元素 绝对定位, p ...
- 《Linux内核修炼之道》精华分享与讨论(5)——Kernel地图:Kconfig与Makefile
转自:http://blog.csdn.net/fudan_abc/article/details/5340408 Makefile不是Make Love 从前在学校,混了四年,没有学到任何东西,每天 ...
- MFC在对话框中的Picture contrl控件中添加icon图标,并改变icon图标的背景色与对话框背景色一致
1.在对话框添加Picture Contrl 控件 2.选中控件,修改ID为IDC_STATIC_PICTURE 和 Type属性为icon 其图标改为 3.添加变量m_picture变量名是灰色,说 ...
- 可移植的配置visual studio工程第三方库
现在编程有太多的好用的第三方库,例如 计算机视觉方面的opencv c++的扩充库boost 特殊的第三方库,相机库,通讯库等 使用这些库给我们带来了极大的便利,同时也有很多困扰.这个工程在我电脑上明 ...