June 2018
Intermediate to advanced
280 pages
7h 46m
English
Functors allow us to apply functions to the given containers. They know how to unwrap the value from the wrapped object, apply the given function, and return another functor containing the resulted/transformed wrapped object. They are useful because they abstract multiple idioms, such as collections, futures (promises), and Optionals. The following code demonstrates the use of the Optional functor from Java, where Optional could be a given value as a result of applying a function to an existing wrapped value (Optional of 5):
jshell> Optional<Integer> a = Optional.of(5);a ==> Optional[5]
Now we apply the function to the wrapped integer object with value of 5 and we get a new; optional holding value of 4.5:
jshell> Optional<Float> ...
Read now
Unlock full access