In the book Functional Programming in Java the author builds a compose function using only the Function<T, U> interface (the interface however is not one shipped with Java 8 but very similar though) the snippet of which is show below
public interface Function<T, U> {
U apply(T arg);
}
though I could understand the method version of compose below, which takes in 2 functions and return a composed 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 get my head around the below compose implementation with 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: couldn't get my mind out of this and move forward with remaining sections :(
Referring to a Function<Integer, Integer> as an integer function, what you have there is a function that maps {an integer function} to {a function mapping an integer function to another integer function}.
Specifically, when given an integer function x, it returns a function that, given an integer function y, returns an integer function that maps an integer z to 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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With