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?
Consider
f1.andThen(f2)
:f1
will take 2 elements and result in only 1f2
will take the result off1
and tranform it to another resultIt's a way to reduce computation
It's more and more usefull when you have several function to use, because there is the same
method
forFunction
, so you can chain them :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:
Guess what
newFunction
does? It adds andThen negates two numbers! See how similar to English this line is:If you call
.apply(1, 2)
, you know you'd get -3.Sure, you could do this without using
andThen
:But look how unreadable that is!
Coding functionally can sometimes make things much easier to read and understand.
It's easier understood with an example:
And the "composed function" is:
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:
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 asf1(f(x, y))
with the ultimate input being the input required byf
, and the ultimate result being the result yielded byf1
.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
andg
, functionf
doing some logic and functiong
doing some other type of logic so when you composef.andThen(g)
that essentially meansg(f(x))
i.e. we first apply the function given as argumentf(x)
and then apply the functiong
to the result.Example:
We first call function
f(10, 10)
and then take the result of that which is20
, pass it to the functiong(20)
and that is executed multiplying20
by2
hence yielding40
.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 inC#
one could simply dog(f(10, 10))
which visibly to the eye is easier to follow, read and understand.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.