Java Reference
In-Depth Information
repeat(3, (Integer x) -> 2*x);
will give x ->(2*(2*(2*x))) or x -> 8*x.
You can test it by writing
System.out.println(repeat(3, (Integer x) -> 2*x).apply(10));
which prints 80.
The method repeat can be coded as follows (note the special case of a zero-trip loop):
Variants of this idea can model richer notions of iteration, including having a functional model
of mutable state passed between iterations. But it's now time to move on; this chapter's role is to
give a summary of functional programming as the basis behind Java 8. There are many excellent
books exploring functional programming in greater depth.
14.6. Summary
Following are the key concepts you should take away from this chapter:
First-class functions are functions that can be passed as arguments, returned as results, and stored in
data structures.
A higher-order function is a function that takes at least one or more functions as input or returns
another function. Typical higher-order functions in Java include comparing , andThen , and
compose .
Currying is a technique that lets you modularize functions and reuse code.
A persistent data structure preserves the previous version of itself when it's modified. As a result, it
can prevent unnecessary defensive copying.
Streams in Java can't be self-defined.
A lazy list is a more expressive version of a Java stream. A lazy list lets you produce elements of the list
on demand by using a supplier that can create more of the data structure.
Pattern matching is a functional feature that lets you unwrap data types. It can be seen as generalizing
Java's switch statement.
 
Search WWH ::




Custom Search