Java Reference
In-Depth Information
Why is this great? From simpler lambda expressions you can represent more complicated
lambda expressions that still read like the problem statement! Note that the precedence of
methods and and or is managed from left to right using their positions in the chain. So
a.or(b).and(c) can be seen as (a || b) && c.
3.8.3. Composing Functions
Finally, you can also compose lambda expressions represented by the Function interface. The
Function interface comes with two default methods for this, andThen and compose, which both
return an instance of Function.
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. For example, given a function f that
increments a number (x -> x + 1) and another function g that multiples a number by 2, you can
combine them to create a function h that first increments a number and then multiplies the
result by 2:
You can also use the method compose similarly to first apply the function given as argument to
compose and then apply the function to the result. For example, in the previous example using
compose, it would mean f(g(x)) instead of g(f(x)) using andThen:
Figure 3.6 illustrates the difference between andThen and compose.
 
Search WWH ::




Custom Search