What is functional programming?
Before we provide the definition, let us revisit the code with elements of functional programming that we have used already in the preceding chapters. All these examples give you a pretty good idea of how a function can be constructed and passed around as a parameter.
In Chapter 6, Data Structures, Generics, and Popular Utilities, we talked about the Iterable
interface and its default void forEach (Consumer<T> function)
method, and provided the following example:
Iterable<String> list = List.of("s1", "s2", "s3");
System.out.println(list); //prints: [s1, s2, s3]
list.forEach(e -> System.out.print(e + " "));//prints: s1 s2 s3
You can see how a Consumer e -> System.out.print(e + " ")
function is passed into the forEach()
method and applied to each element flowing into this method...