OO Programming with Java


Advanced Concepts

The preceding concepts can be apply to other collections type with for instance Trees (e.g. files' structure, XML/HTML documents, etc.):

a. Definition

public class Funs {
  interface Tree<T> {
	<X> Tree<X> map(Function<T,X> f);
    class Leaf<T> implements Tree<T> {
      public <X> Tree<X> map(Function<T,X> f) {
    	return new Leaf();
      }
    }
    class Node<T> implements Tree<T> {
      private T v;
      private Tree<T> l,r;
      public Node(Tree<T> l,T v, Tree<T> r)
        { this.l=l; this.v=v; this.r=r; }
      public <X> Tree<X> map(Function<T,X> f) {
    	return new Node(l.map(f),f.apply(v),r.map(f));
      }
    }
  }

Notice the use of inner classes to hide implementation of trees.
Exercise: use an UML classes' diagram to well understand the previous code.


15 - 17