The implementation is done using only the <> function in java 8

In the book "Functional Programming in Java", the author creates a linking function using only the interface Function<T, U>(the interface, however, is not one sent with Java 8, but it is very similar, though), a fragment of which is shown below

public interface Function<T, U> {
  U apply(T arg);
}

although I could understand the version of the method described below, which takes 2 functions and returns a folded function

public static final Function<Integer, Integer> compose (final Function<Integer, Integer> f1, 
                                   final Function<Integer, Integer> f2) {
         arg -> f1.apply(f2.apply(arg));
}

I just couldn't come up with an implementation using the <> function and lambdas

static Function<Function<Integer, Integer>,
            Function<Function<Integer, Integer>,
                    Function<Integer, Integer>>> compose =
            x -> y -> z -> x.apply(y.apply(z));

PS: I could not escape from this and move forward with the rest of the sections :(

+4
source share
3 answers

Function<Integer, Integer> , , { } {, }.

, x, , , y, , z x(y(z)).

Given a function x
Returns a function that:
    Given a function y
    Returns a function that:
        Given an integer z
        Returns x(y(z))
+6

λ-, , λx.E , x E. , λx. λy. x+y - , x λy. x+y, , y x+y. λ(x,y).E , x y E. , λ(x,y). x+y - , x y x+y.

"two arguments at once" compose: λ(f,g). λx.f(g(x))

" ": λf. λg. λx.f(g(x))

, Java, - . f g ( compose) Function < Integer, Integer >. T. compose Function < T, Function < T, T > >.

P.S. , λx. λy. E, curried, λ(x,y). E - . .: https://en.wikipedia.org/wiki/Currying

+3

/ .

Function<Function<R, S>,
         Function<Function<T, R>,
                  Function<T, S>>> compose = x -> y -> z -> x.apply(y.apply(z));

edit: ( , )

0

All Articles