June 2018
Intermediate to advanced
280 pages
7h 46m
English
In mathematics, functions are composed/chained together by using the output of a function as the input of the next. The same rule applies in functional programming, where first-class functions are used by higher-order functions. The preceding code already contains such an example—see the use of the convF2C pure function inside the map function.
To make the function composition more visible, we may rewrite the conversion formula by making use of the andThen method:
jshell> IntUnaryOperator convF2C = ((IntUnaryOperator)(x -> x-32)).andThen(x -> x *5).andThen(x -> x / 9);convF2C ==> java.util.function.IntUnaryOperator$$Lambda$29/1234776885@dc24521jshell> convF2C.applyAsInt(80);$23 ==> 26
Read now
Unlock full access