有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在匿名类中表示curry的java方法(从lambda表达式转换)。嵌套函数

嗨,我只是想检查一下我的实现和理解是否正确。我试图将一种嵌套的lambda转换成一个anon类 Function<Integer,Function<Integer,Function<Integer,Integer>>> h = x -> y -> z-> x + y + z

这是将其表示为anon类的适当方式吗

Function<Integer,Function<Integer,Function<Integer,Integer>>> h = new Function <> ( 
int x, y;
@Override 
Function<Integer,Function<Integer,Integer>> apply ( Integer x)

              return y-> z-> (x+y+z);
   }
};

共 (1) 个答案

  1. # 1 楼答案

    为了将lambda转换为匿名函数,您应该一点一点地分解它,每个->对应一个匿名函数实例化,这将导致如下结果:

    var h = new Function<Integer,Function<Integer,Function<Integer,Integer>>>() {
      public Function<Integer,Function<Integer,Integer>> apply(Integer x) {
        return new Function<Integer,Function<Integer,Integer>>() {
          public Function<Integer,Integer> apply(Integer y) {
            return new Function<Integer,Integer>() {
              public Integer apply(Integer z) {
                return x + y + z;
              }
            };
          }
        };
      }
    };