`
Eastsun
  • 浏览: 303899 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

Java与Scala中的闭包

阅读更多
  原文地址Closures in Java and Scala
  翻  译Eastsun


  People argue that verbose code is easier to understand. Do you agree when reading these two examples, one method in Java, one in Scala?
  人们普遍认为,详细的代码更易于理解。但如果你阅读下面两段代码--一个使用Java另一个使用Scala--你是否还这样认为呢?
public List<Item> bought(User user) {
    List<Item> result = new ArrayList();
    for (Item item : currentItems) {
        if (user.bought(item)) {
            result.add(item);
        }
    }
    return result;
}

def bought(user: User) = items.filter(user bought _)


  If you are familiar with Java, which is more likely then you being familiar with Scala, you may tend to the Java-version. Scala has a different syntax for declaring types and generics, and supports closures, including anonymous parameters (the underscore).
  如果你熟悉Java,那么很可能你将会了解到Scala。也许你会倾向于使用Java的版本。Scala具有不同的类型声明以及泛型语法,并且支持闭包,以及匿名参数(下划线)。

  When Closures are introduced in one of the next Java releases, JDK 7 or 8, the example could be rewritten like this:
  在Java的后续版本中引入闭包后(JDK7或JDK8),上面Java版的代码可以重写为:
public List<Item> bought(User user) {   
    return ListUtils.filter(Item item : items ) {      
        user.bought(item);    
    }
}

  Or with extension methods:
  或者使用方法扩展
public List<Item> bought(User user) { 
    return items.filter(Item item : )  { 
        // <-- note the smily! thats what its all about!
        user.bought(item);
    }
}

  The interesting differences between Java with closures and Scala is the static typing: Scala inferences that items are all of type Item, so there is no need to specify that information again.

  带有闭包的Java与Scala一个让人感兴趣的区别是它们的静态类型:Scala的类型推断能力能得到其所有变量的类型,因此不需要再指明这一点。

  So, while the current Java Closures specification is a great step in the right direction, lead by Neil Gafter, who is quite the right man for the job, it may not be worth to wait for it, if you have the choice. Its not even sure yet that we'll see that spec implemented in JDK7, and even that is at best one and a half year away.
  因此,即便目前的JAVA闭包规范Neil Gafter的领导下正朝着正确的方向做出了很大的进步;但我们也未必需要去等待它,如果我们已经有了其他选择。何况JDK7中会不会实现闭包现在还没有确定--即便是,那也至少是一年半以后的事了。
分享到:
评论
2 楼 bloodrate 2008-08-18  
我说说我对闭包的理解,看看片面不,我理解的闭包是一个处理的输入和输出类型项相同,这样这次的输出就可以作为下次的输入在次处理。
说这个我想到了关于“关系数据库”和“对象数据库”的争论,关系数据库存储的数据,通过应用系统的持久层转化为对象模型是有很大困难的,这其中原因在于“关系”和“对象”的存储方式不同,但是关系模型之所以流行,和sql语句密不可分,sql本身遵循闭包原则,致使一次查询结果可以作为下次查询条件,人们可以指学习少量关键词就能拼装成很复杂的查询语句,闭包是数学名词,有着数学上的天然对称美。
至于java语言的闭包,我不完全理解,不过我相信就算jdk还没有提供,使用者也可以自己撰写自己的闭包类,现在我考虑的是java会不会加入语义级别的闭包。
1 楼 hity 2008-08-16  
刚刚试验了下java7:
class A{
    static { => int } answer = { => 42 };
    public static void main(String[] args) {
      int i = answer.invoke();
      System.out.println(i);
      int temp=new A().add({int x,int y=>x*x+y*y});
      System.out.println(temp);
    }
     public int add({ int,int=>int } s){
     return s.invoke(3, 4);
     }

}

相关推荐

    Scala程序设计(第2版)

    22.1 在Scala代码中使用Java名称 430 22.2 Java泛型与Scala泛型 430 22.3 JavaBean的性质 432 22.4 AnyVal类型与Java原生类型 433 22.5 Java代码中的Scala名称 433 22.6 本章回顾与下一章提要 434 ...

    快学 scala 中文版 带完整目录

    3.8 与Java的互操作 48 练习 49 第4章 映射和元组 A1 53 4.1 构造映射 53 4.2 获取映射中的值 54 4.3 更新映射中的值 55 4.4 迭代映射 56 4.5 已排序映射 57 4.6 与Java的互操作 57 4.7 元组 58 4.8 拉链...

    jdk-8u181-windows-x64.zip

    jdk1.8新特性,例如:Lambda表达式(也称为闭包)是整个Java 8发行版中最受期待的在Java语言层面上的改变,Lambda允许把函数作为一个方法的参数(函数作为参数传递进方法中),或者把代码看成数据:函数式程序员对这...

    孢子:Scala孢子,安全的移动式封闭

    Spores是Scala编译器的扩展,可以在并发和分布式环境中更安全地使用闭包。 它使开发人员可以保证基于类型的功能属性,从而可以更好地控制功能环境。 Spores带有传递检查器,以确保JVM可以对捕获的类型进行序列化...

    做Java开发这一年

    从去年到现在,从.NET转向Java开发(只是因为项目原因,绝对与平台好坏没有关系)差不多有一年的时间了。通过这一年时间也有些感触,想从几个面比较一下这两个平台。希望能做到客观公正。我原来是使用C#语言的,和...

    jgo:JGo - Golang 的 Java 编译器和运行时环境

    jgoc 编译器用 Scala 编写,运行时用 Java 编写。 Drone.io 持续集成服务 吉特聊天 为什么是JVM? 选择 JVM 的动机是: 兼容性和 JVM 调试/分析 许多优秀的库都是为 JVM 编写的。 希望通过让现有 Go 用户能够使用...

    借贷管理源码java-embedded-kafka:一个提供内存Kafka实例来运行测试的库

    丑管理源码java 嵌入式卡夫卡 一个提供内存 Kafka 实例来运行测试的库。 灵感来自 . 版本兼容性矩阵 ...实例的代码包含在withRunningKafka闭包中。 一个例子,使用 ScalaTest: class MySpec extends An

    javalruleetcode-Coding:记录编码问题、解决方案、解释、测试用例等

    C++、Java、Python、C#、Scala 目录 1. 数学 1.1 离散数学 1.1.1 图论 1.1.1.1 图遍历(BFS、DFS) 1.1.1.2 最小生成树(Prim、Kruskal) 1.1.1.3 最短路径(Dijkstra、Floyd) 1.1.1.4 最长路径 1.1.1.5 传递闭包 ...

Global site tag (gtag.js) - Google Analytics