Java Reference
In-Depth Information
a stream is anything actually computed. This has the great advantage when you apply several
operations (perhaps a filter and a map followed by a terminal operation reduce) to a stream;
then the stream has to be traversed only once instead of for each operation.
In this section we consider the notion of lazy lists, which are a form of a more general stream
(lazy lists form a similar concept to stream). Lazy lists also provide an excellent way of thinking
about higher-order functions; you place a function value into a data structure so most of the
time it can sit there unused, but when it's called (that is, on demand) it can create more of the
data structure. Figure 14.5 illustrates this idea.
Figure 14.5. Elements of a LinkedList exist (are spread out) in memory.
But elements of a LazyList are created on demand by a Function —you can
see them as spread out in time.
Enough talking—let's see how this works. What you want to achieve is to generate an infinite list
of prime numbers using the algorithm we described earlier.
A basic linked list
Recall that you can define a simple linked-list-style class called MyLinkedList in Java by writing
it as follows (here we only consider a minimal MyList interface):
interface MyList<T> {
T head();
MyList<T> tail();
default boolean isEmpty() {
return true;
}
 
Search WWH ::




Custom Search