如何从Function接口返回Consumer。

huangapple go评论62阅读模式
英文:

how to return Consumer from Function interface

问题

以下是翻译的代码部分:

public static void main(String[] args) {

    Function<Integer, Integer> factorial = n -> IntStream.rangeClosed(2, n)
            .reduce(1, (x, y) -> x * y);

    Function<Integer, Consumer<Integer>> f3 = n -> {
        return x -> System.out.println(factorial.apply(x * factorial.apply(n)));
    };

    f3.apply(5).accept(2); // 输出 0
}

请问您还有其他需要帮助的地方吗?

英文:

I have learning functional interfaces. I have written below code to return a Consumer from Function interface but it's not working. It's returning output 0. I don't understand why it returning0.

Code:

public static void main(String[] args) {

    Function&lt;Integer, Integer&gt; factorial = n -&gt; IntStream.rangeClosed(2, n)
            .reduce(1, (x, y) -&gt; x * y);

    Function&lt;Integer, Consumer&lt;Integer&gt;&gt; f3 = n -&gt; {
        return x -&gt; System.out.println(factorial.apply(x * factorial.apply(n)));
    };

    f3.apply(5).accept(2); // output 0
}

Can someone explain why this is(f3.apply(5).accept(2)) returning 0. Is there any other way to implement this.

答案1

得分: 3

public static void main(String[] args) throws IOException {
    Function<Integer, BigInteger> factorial = n -> {
        BigInteger res = BigInteger.ONE;

        for (int i = 2; i <= n; i++)
            res = res.multiply(BigInteger.valueOf(i));

        return res;
    };

    Function<Integer, Consumer<Integer>> f3 = n -> {                
        return (Consumer<Integer>)x -> {                            
            BigInteger fact = factorial.apply(n);                  
            fact = fact.multiply(BigInteger.valueOf(x));            
            System.out.println(factorial.apply(fact.intValue()));   
        };
    };

    f3.apply(5).accept(2); 
}
英文:
public static void main(String[] args) throws IOException {
    Function&lt;Integer, BigInteger&gt; factorial = n -&gt; {
        BigInteger res = BigInteger.ONE;

        for (int i = 2; i &lt;= n; i++)
            res = res.multiply(BigInteger.valueOf(i));

        return res;
    };

    Function&lt;Integer, Consumer&lt;Integer&gt;&gt; f3 = n -&gt; {                // n = 5
        return (Consumer&lt;Integer&gt;)x -&gt; {                            // x = 2
            BigInteger fact = factorial.apply(n);                   // 120 - correct
            fact = fact.multiply(BigInteger.valueOf(x));            // 240
            System.out.println(factorial.apply(fact.intValue()));   // too big for int and long
        };
    };

    f3.apply(5).accept(2); // 4067885363647058120493575921486885310172051259182827146069755969081486918925585104009100729728348522923820890245870098659147156051905732563147381599098459244752463027688115705371704628286326621238456543307267608612545168337779669138759451760395968217423617954330737034164596496963986817722252221059768080852489940995605579171999666916004042965293896799800598079985264195119506681577622056215044851618236292136960000000000000000000000000000000000000000000000000000000000
}

答案2

得分: 1

得到Consumer变量后,您需要将代码拆分为两部分

Consumer<Integer> c = f3.apply(2);
// x -> System.out.println(factorial.apply(x * factorial.apply(5)))

c.accept(2);

从这里您可以看出,有些东西无法找到,因为您的consumer将执行(x * 5!)!,这是(120x)!,所以对于2 -> 240!约为10^468,而整数只能保存最多2^32


我建议您去掉一个层次的factorial以获得更容易理解的结果

Function<Integer, Consumer<Integer>> f3 = n -> x -> {
    System.out.println(x * factorial.apply(n));
};

Consumer<Integer> c = f3.apply(5);

c.accept(1); // 120
c.accept(2); // 240
c.accept(3); // 360
c.accept(4); // 480
英文:

To get the Consumer in a variable, you need to split your code in 2 parts

Consumer&lt;Integer&gt; c = f3.apply(2);
//x -&gt; System.out.println(factorial.apply(x * factorial.apply(5)))

c.accept(2);

From here you see that something is not gonna be find, as your consumer will do (x * 5!)! which is (120x)! so with 2 -> 240!about 10^468, where an integer can hold only up to 2^32


I'd suggest you remove a level of factorial to get easier results to understand

Function&lt;Integer, Consumer&lt;Integer&gt;&gt; f3 = n -&gt; x -&gt; {
    System.out.println(x * factorial.apply(n));
};

Consumer&lt;Integer&gt; c = f3.apply(5);

c.accept(1); // 120
c.accept(2); // 240
c.accept(3); // 360
c.accept(4); // 480

huangapple
  • 本文由 发表于 2020年9月6日 22:46:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63765406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定