June 2018
Intermediate to advanced
280 pages
7h 46m
English
Higher-order functions are functions that can take other functions as parameters, create, and return them. They promote code reuse by making use of existing and already-tested small functions. For example, in the following code, we calculate the average in Celsius for the given temperatures in Fahrenheit:
jshell> IntStream.of(70, 75, 80, 90).map(x -> (x - 32)*5/9).average();$4 ==> OptionalDouble[25.5]
Notice the use of the lambda expression inside the higher-order map function. The same lambda expression can be utilized in multiple places to convert temperatures.
jshell> IntUnaryOperator convF2C = x -> (x-32)*5/9;convF2C ==> $Lambda$27/1938056729@4bec1f0cjshell> IntStream.of(70, 75, 80, 90).map(convF2C).average();
Read now
Unlock full access