default method in Java.util.function.Function
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (T t) -> after.apply(apply(t));}default <V> Function<V, R> compose(Function<? super V, ? extends T> before) { Objects.requireNonNull(before); return (V v) -> apply(before.apply(v));}
main
Function<Integer, Integer> doubleFunction = x -> x * 2;Function<Integer, Integer> squareFunction = x -> x * x;int result1 = doubleFunction.apply(5); // 10int result2 = squareFunction.apply(5); // 25Function<Integer, Integer> integerIntegerFunction = doubleFunction.andThen(squareFunction);int result3 = doubleFunction.andThen(squareFunction).apply(5); // 100int result4 = doubleFunction.compose(squareFunction).apply(5); // 50
Question: statement- doubleFunction.compose(squareFunction).apply(5)
, doubleFunction.compose
returns (V v) -> apply(before.apply(v))
, how does the outer code apply(...)
run?
before.apply(v)
returns int number, assume returns symbol res. For (V v) -> apply(res)
, how this can be regard as doubleFunction's instantiation call.【pool English, sry】