Java Reference
In-Depth Information
filterApples in
chapters 1
and
2
.
But this was just a start. Another interesting example was the
use of the static method Comparator.comparing, which takes a function as parameter and
returns another function (a Comparator), as illustrated in the following code and
figure 14.1
:
Figure 14.1.
comparing
takes a function as parameter and returns
another function.
Comparator<Apple> c = comparing(Apple::getWeight);
We did something similar when we were composing functions in
chapter 3
to create a pipeline of
operations:
Function<String, String> transformationPipeline
= addHeader.andThen(Letter::checkSpelling)
.andThen(Letter::addFooter);
Functions (like Comparator.comparing) that can do at least one of the following are called
higher-order functions
within the functional programming community:
Take one or more functions as parameter
Return a function as result
This directly relates to Java 8 functions because they can not only be passed as arguments but
also returned as results, assigned to local variables, or even inserted into structures. For
example, a pocket calculator program might have a Map<String, Function<Double, Double>>,
which maps the String “sin” to Function<Double, Double> to hold the method reference
Math::sin. We did something similar when we introduced the factory design pattern in
chapter
8
.
differentiation as
Search WWH ::
Custom Search