闭包(Closures)在各种语言中的例子 http://blog.csdn.net/ruby_cn/archive/2004/11/23/192588.aspx
来源:互联网 发布:创远家居装修系统源码 编辑:程序博客网 时间:2024/05/01 04:49
作者:<liubin <AT>huangpuzhuang.com>
http://www.ruby-cn.org/
2004/11/23
在Martin Fowler的一篇关于闭包的文章中(http://martinfowler.com/bliki/Closures.html,中文版:http://www.ruby-cn.org/articles/closures.html),提到了一个例子,主要是用Ruby语言写的,后来网上出现了很多其它语言的版本,本文将各个版本摘录下来,方便大家阅读比较。
1.Ruby
摘自原文http://martinfowler.com/bliki/Closures.html
def managers(emps) return emps.select {|e| e.isManager}end
2.
def highPaid(emps) threshold = 150 return emps.select {|e| e.salary > threshold}end
3.
def paidMore(amount) return Proc.new {|e| e.salary > amount}end
4.
highPaid = paidMore(150)john = Employee.newjohn.salary = 200print highPaid.call(john)
2.c# 2.0
摘自:http://joe.truemesh.com/blog//000390.html
1.
public List<Employee> Managers(List<Employee> emps) { return emps.FindAll(delegate(Employee e) { return e.IsManager; }}
2.
public List<Employee> HighPaid(List<Employee> emps) { int threshold = 150; return emps.FindAll(delegate(Employee e) { return e.Salary > threshold; });}
3.
public Predicate<Employee> PaidMore(int amount) { return delegate(Employee e) { return e.Salary > amount; }}
4.
Predicate<Employee> highPaid = PaidMore(150);Employee john = new Employee();john.Salary = 200;Console.WriteLine(highPaid(john));
3.Python
摘自:http://ivan.truemesh.com/archives/000392.html
直接用lambda函数
1.
def managers(emps):
return filter(lambda e: e.isManager, emps)
2.
def highPaid(emps):
threshold = 150
return filter(lambda e: e.salary > threshold, emps)
3.
def paidMore(amount):
return lambda e: e.salary > amount
4.
highPaid = paidMore(150)
john = Employee()
john.salary = 200
print highPaid(john)
另一种方式,用列表包含(list comprehensions)
1.
def managers(emps):
return [e for e in emps if e.isManager]
2.
def highPaid(emps):
threshold = 150
return [e for e in emps if e.salary > threshold]
Chris Reedy 提供了另一种避免使用lambda的方法:
def paidMore(amount): def paidMoreCheck(e): return e.salary > amount return paidMoreCheck
4.Java
摘自: http://joe.truemesh.com/blog//000390.html
jdk 1.5支持
private class FindHighPaidEmployees extends Algorithms { Constraint highPaidEmployee; public FindHighPaidEmployess(Constraint constraint) { this.highPaidEmployee = constraint; } public Object call(Object o) { List emps = (List)o; return findAll(emps, highPaidEmployee); }}private class PaidMore implements Constraint { private int salary; public PaidMore(int salary) { this.salary = salary; } public boolean test(Object o) { return ((Integer)o).intValue() > salary; }}
Closure highPaidFinder = new FindHighPaidEmployees(new PaidMore(150));List highPaidEmployees = highPaidFinder.call(myEmployees);
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=192588
- 闭包(Closures)在各种语言中的例子 http://blog.csdn.net/ruby_cn/archive/2004/11/23/192588.aspx
- 闭包(Closures)在各种语言中的例子
- http://blog.csdn.net/CYRTSOFT/archive/2004/12.aspx
- http://blog.csdn.net/mrunix/archive/2004/08.aspx
- 虚拟平台在嵌入式中的应用(转自雪松http://blog.csdn.net/coolbacon/archive/2011/04/11/6316403.aspx#1638545)
- eclipse技巧 http://blog.csdn.net/narilee/archive/2004/11/16/183520.aspx
- 常用:http://blog.csdn.net/rickjelly2004/archive/2004/11/03/165514.aspx
- 网页特效代码 (转自http://blog.csdn.net/arielxp/archive/2004/10/20/143752.aspx)
- Flex学习资料(转 http://blog.csdn.net/tekkenvs11/archive/2009/06/23/4289195.aspx)
- setprecision()与setiosflags() (转载http://blog.csdn.net/yunhaiC/archive/2008/03/11/2167462.aspx)
- SqlDataAdapter Update(转自:http://blog.csdn.net/dingjinze/archive/2008/11/26/3379743.aspx)
- [转] 在ASP.NET下用Microsoft Excel进行数据分析与报表(ASP.Net)[http://blog.csdn.net/hoker_long/archive/2004/09/21/112470.aspx]
- linux操作系统下c语言编程入门(http://blog.csdn.net/CLX1314/archive/2006/03.aspx)
- http://blog.csdn.net/hbuzhang/archive/2006/01.aspx
- http://blog.csdn.net/zhuky/archive/2009/12.aspx
- 让插入到innerHTML中的script跑起来(转http://blog.csdn.net/lee576/archive/2008/11/20/3341146.aspx)
- 我心目中的牛程序员---http://blog.csdn.net/jirigala/archive/2010/03/02/5340655.aspx
- SQL Server连接中的三个最常见错误(源于http://blog.csdn.net/goody9807/archive/2004/12/03/203087.aspx)
- Asp过滤注入函数2
- 第五次作业
- 百鸡问题
- [SOA介绍]什么是SOA
- Asp连接Sql数据库或者Access函数
- 闭包(Closures)在各种语言中的例子 http://blog.csdn.net/ruby_cn/archive/2004/11/23/192588.aspx
- Asp关闭数据库连接函数
- PHP常见问题汇总
- 对 "闭包-closure" 的一些见解http://wj.cnblogs.com/archive/2006/04/22/381851.html
- 华为收购港湾核心业务 有恶意收购嫌疑
- 广州番禺大夫山森林公园游记
- 在Javascript中,什么是闭包(Closure)
- 开发网页程序的时候,JS经常报错该如何处理?
- 第五次作业