可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
There's a default method andThen()
in the BiFunction
interface (java.util.function
package).
default <V> BiFunction<T,U,V> andThen(Function<? super R,? extends V> after)
The documentation says:
Returns a composed function that first applies this function to its input, and then applies the after function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the composed function.
It's little confusing to understand what the explanation means. As per my understanding, a composed function is returned when the default andThen()
method is invoked. This composed function is invoked on the types T
and U
that returns the type V
. Finally, there's and after
function that is invoked on the types R
and V
.
What's the need of this method? How does it actually fit in the picture?
回答1:
It's little confusing to understand what the explanation means.
To explain it as simple as I can, the method andThen
returns a function that first applies a given function to an input and then applies another function to the result of that application.
Assume we had two functions f
and g
, function f
doing some logic and function g
doing some other type of logic so when you compose f.andThen(g)
that essentially means g(f(x))
i.e. we first apply the function given as argument f(x)
and then apply the function g
to the result.
Example:
BiFunction<Integer, Integer, Integer> f = Math::addExact;
Function<Integer, Integer> g = e -> e * 2;
System.out.println(f.andThen(g).apply(10,10)); // 40
We first call function f(10, 10)
and then take the result of that which is 20
, pass it to the function g(20)
and that is executed multiplying 20
by 2
hence yielding 40
.
To be honest the syntax to call a function in Java
is not the best it can be so I can understand when someone looks at this the first time it might be difficult to grasp and gets harder to follow the more you compose functions, for example in C#
one could simply do g(f(10, 10))
which visibly to the eye is easier to follow, read and understand.
What's the need of this method? How does it actually fit in the
picture?
In my experience, it's not common that I've composed functions as shown above but a typical scenario I could imagine is if you have various utility methods that do some logic where the result of one function is further passed to other functions for processing in which case you can then use function composition to create various transformation pipelines by composing the utility methods.
回答2:
I think the main purpose of the andThen
function is to make your code more readable and more functional.
Let's look at and example:
BiFunction<Integer, Integer, Integer> add = (x, y) -> x + y;
Function<Integer, Integer> negate = x -> -x;
BiFunction<Integer, Integer, Integer> newFunction = add.andThen(negate);
Guess what newFunction
does? It adds andThen negates two numbers! See how similar to English this line is:
BiFunction<Integer, Integer, Integer> newFunction = add.andThen(negate);
If you call .apply(1, 2)
, you know you'd get -3.
Sure, you could do this without using andThen
:
BiFunction<Integer, Integer, Integer> newFunction = (x, y) -> negate.apply(add.apply(x, y))
But look how unreadable that is!
Coding functionally can sometimes make things much easier to read and understand.
回答3:
Consider f1.andThen(f2)
:
- First,
f1
will take 2 elements and result in only 1
- after that,
f2
will take the result of f1
and tranform it to another result
BiFunction<Integer, Integer, Integer> plus10 = (i1, i2) -> i1 + i2 + 10;
Function<Integer, Integer> mult = i -> i * 5;
System.out.println(plus10.andThen(mult).apply(5, 6)); // (5+6+10) *5 = 105
It's a way to reduce computation
int val1 = plus10.apply(5, 6);
int res1 = mult.apply(val1);
int res2 = plus10.andThen(mult).apply(5, 6);
System.out.println(res1 == res2); //true
It's more and more usefull when you have several function to use, because there is the same method
for Function
, so you can chain them :
System.out.println(plus10.andThen(mult).andThen(mult).andThen(mult).apply(5, 6));
// (5+6+10)*5*5*5 = 2625
回答4:
It's easier understood with an example:
BiFunction<Integer, Integer, String> f =
(n1, n2) -> String.format("result is %s", n1+n2);
And the "composed function" is:
BiFunction<Integer, Integer, String> f1 =
f.andThen(string -> string.toUpperCase());
Note that the second function still takes the same argument types as the first one, although it internally only needs a String to execute its logic.
Consider the invocation:
System.out.println(f1.apply(2, 3));
Which outputs RESULT IS 5
, that is: it calls first function and then calls the second function with the result of the first. So it's simply understood as f1(f(x, y))
with the ultimate input being the input required by f
, and the ultimate result being the result yielded by f1
.