我能否在Java 8中使用Clojure函数作为Lambdas? [英] Will I be able to use Clojure functions as Lambdas in Java 8?

查看:179
本文介绍了我能否在Java 8中使用Clojure函数作为Lambdas?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Clojure中使用了许多库,这些库产生更高阶的函数,符合 clojure.lang.IFn 界面。

它有多个重载重载,即I.e.接口看起来像这样:

  public interface IFn extends Callable,Runnable {
public Object invoke();
public Object invoke(Object arg1);
public Object invoke(Object arg1,Object arg2);
public Object invoke(Object arg1,Object arg2,Object arg3);
....等
public Object applyTo(ISeq arglist);
}

我能否在Java 8中直接使用此类对象作为可调用对象lambda函数?

解决方案

你是什么意思,使用这种类型的对象作为可调用lambdas?

在非常简单的情况下,Java 8 lambda可以被认为是语法糖+某些类型的接口的匿名类的类型推断,也就是 em>功能界面 [1] a>:


上面使用的ActionListener接口只有一个方法。许多
通用回调接口都具有此属性,例如Runnable和
Comparator。我们给所有只有一个方法的接口一个
名称:功能接口。


备注:lambdas真的不是只是一个糖;在内部它们的实现方式与匿名类不同,并且也存在一些语义差异;请参阅关于ProgrammersExchange的这个极好的答案,以获得更多关于此问题的信息。然而,在这个问题和答案中,这并不重要。



任何需要某个函数接口的值的地方(方法参数,局部变量,字段声明等等)可以使用简短的语法来创建实现此方法的匿名类的实体,也就是lambda表达式:

  Runnable r =() - > {
System.out.println(Hi);
};
//相当于
Runnable r = new Runnable(){
public void run(){
System.out.println(Hi);
}
};

public interface函数< F,T> {
T call(F arg);
}

函数< String,char []> c = s - > (<+ s +>)。toCharArray();
//相当于
函数< String,char []> c = new Function<>(){
public char [] call(String s){
return(<+ s +>)。toCharArray();
}
};

因此,您的问题只能通过以下方式解释:是否可以创建类型为 IFn 使用Java 8 lambda语法?



答案是否定的。 Lambda语法仅适用于功能接口。 clojure.lang.IFn 不是一个函数接口,因为它包含的不仅仅是单个方法,所以不可能做类似于

  IFn f =(String s) - > s.toLowerCase(); 


I use a number of libraries in Clojure that produce higher order functions that conform to the "clojure.lang.IFn" interface.

It has multiple arity overloads, I.e. the interface looks something like:

public interface IFn extends Callable, Runnable{
  public Object invoke() ;
  public Object invoke(Object arg1) ;
  public Object invoke(Object arg1, Object arg2) ;
  public Object invoke(Object arg1, Object arg2, Object arg3) ;
  .... etc.
  public Object applyTo(ISeq arglist) ;
}

Will I be able to use objects of this type directly in Java 8 as callable lambda functions?

解决方案

What do you mean, use objects of this type as callable lambdas?

In very simple cases, Java 8 lambdas can be thought as syntactic sugar + some type inference for anonymous classes for certain type of interfaces, namely functional interfaces [1]:

The interface ActionListener, used above, has just one method. Many common callback interfaces have this property, such as Runnable and Comparator. We'll give all interfaces that have just one method a name: functional interfaces.

Remark: lambdas are really not just a sugar; internally they are implemented differently than anonymous classes, and there are also some semantic differences; see this excellent answer on ProgrammersExchange for more on this matter. However, this is not really important in context of this question and answer.

Anywhere where a value of some functional interface is expected (method argument, local variable, field declaration etc.) it will be possible to use short syntax for creating an entity resemling anonymous class implementing this method, that is, a lambda expression:

Runnable r = () -> {
    System.out.println("Hi");
};
// Equivalent to
Runnable r = new Runnable() {
    public void run() {
        System.out.println("Hi");
    }
};

public interface Function<F, T> {
    T call(F arg);
}

Function<String, char[]> c = s -> ("<" + s + ">").toCharArray();
// Equivalent to
Function<String, char[]> c = new Function<>() {
    public char[] call(String s) {
        return ("<" + s + ">").toCharArray();
    }
};

So your question can only be interpreted in the following way: is it possible to create objects of type IFn using Java 8 lambda syntax?

The answer is no. Lambda syntax is possible only with functional interfaces. clojure.lang.IFn is not a functional interface because it contains much more than single method, so it won't be possible to do something like

IFn f = (String s) -> s.toLowerCase();

这篇关于我能否在Java 8中使用Clojure函数作为Lambdas?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆