File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package section21 ;
22
33/**
4- * This interface encapsulates a function that maps type T to U, and it can be used for funciton -as-value as in
4+ * This interface encapsulates a function that maps type T to U, and it can be used for function -as-value as in
55 * functional programming.
66 *
7- * Java 8 now includes Function<T,U> under java.util.function package.
7+ * Java 8 now includes Function<T,U> under java.util.function package for exactly this .
88 */
99public interface MapperFn <T ,U > {
1010
Original file line number Diff line number Diff line change 55/**
66 * This class implements a generic doubly-linked list with common operations.
77 */
8- public class MyLinkedList <T > implements Iterable <T > {
8+ public class MyLinkedList <T > implements MyList <T > {
99
1010 /**
1111 * Static nested class that presents a node in the linked list; also generic.
@@ -147,6 +147,15 @@ public T getTailValue() {
147147 return tail .value ;
148148 }
149149
150+ /* Creates a new linked list by mapping each of its elements */
151+ public <U > MyList <U > map (MapperFn <T ,U > fn ) {
152+ MyLinkedList <U > newList = new MyLinkedList <>();
153+ for (T elem : this ) {
154+ newList .append (fn .call (elem ));
155+ }
156+ return newList ;
157+ }
158+
150159 /* Get an iterator for the current state of the linked list */
151160 public Iterator <T > iterator () {
152161 return new Iterator <T >() {
Original file line number Diff line number Diff line change 1+ package section21 ;
2+
3+ import java .io .PrintWriter ;
4+
5+ /**
6+ * A generic printable list that extends MyLinkedList<T> and adds a print() method to make it printable.
7+ * Type argument constraint (`T extends PrintableObj`) makes sure that each element is printable.
8+ */
9+ public class MyPrintableList <T extends PrintableObj > extends MyLinkedList <T > implements PrintableObj , MyList <T > {
10+
11+ /* This method prints all contents of the linked list to the PrintWriter */
12+ public void print (PrintWriter fs ) {
13+ for (T elem : this ) { // this syntax is possible since MyLinkedList implements MyList<T> (hence Iterable<T>)
14+ fs .print (elem );
15+ }
16+ }
17+
18+ }
19+
20+ /**
21+ * This interface represents an object that has print method that prints itself to the PrintWriter as provided.
22+ */
23+ interface PrintableObj {
24+
25+ /**
26+ * Prints itself to the provided PrintWriter.
27+ * @param fs target PrintWriter
28+ */
29+ void print (PrintWriter fs );
30+ }
Original file line number Diff line number Diff line change 1+ package section21 ;
2+
3+ import java .io .PrintWriter ;
4+
5+ /**
6+ * This example uses MyPrintableList declared earlier.
7+ */
8+ public class MyPrintableListExample {
9+
10+ public static void main (String [] argv ) {
11+ MyPrintableList <PrintableInteger > myList = new MyPrintableList <>();
12+ myList .append (new PrintableInteger (2 ));
13+ myList .append (new PrintableInteger (3 ));
14+ myList .append (new PrintableInteger (5 ));
15+ myList .append (new PrintableInteger (7 ));
16+
17+ PrintWriter stdout = new PrintWriter (System .out );
18+ PrintWriter stderr = new PrintWriter (System .err );
19+ myList .print (stdout );
20+ myList .print (stderr );
21+ stdout .flush ();
22+ stderr .flush ();
23+ }
24+ }
25+
26+ /**
27+ * PrintableInteger simply holds an immutable integer, and defines a toString() method to be used by print(PrintWriter).
28+ */
29+ class PrintableInteger implements PrintableObj {
30+
31+ public final int val ; // intentionally immutable
32+
33+ public PrintableInteger (int val ) {
34+ this .val = val ;
35+ }
36+
37+ public void print (PrintWriter fs ) {
38+ fs .write (val ); // what's written to `fs` is determined by its toString method
39+ }
40+
41+ public String toString () {
42+ return String .format ("PrintableInteger(val=%d)" , val );
43+ }
44+ }
You can’t perform that action at this time.
0 commit comments