Esempio n. 1
0
 private static <A> Stream<String> drawSubTrees(final Show<A> s, final Stream<Tree<A>> ts) {
   return ts.isEmpty()
       ? Stream.nil()
       : ts.tail()._1().isEmpty()
           ? shift("`- ", "   ", ts.head().drawTree(s)).cons("|")
           : shift("+- ", "|  ", ts.head().drawTree(s)).append(drawSubTrees(s, ts.tail()._1()));
 }
Esempio n. 2
0
 /**
  * Puts the elements of the tree into a Stream, in pre-order.
  *
  * @return The elements of the tree in pre-order.
  */
 public Stream<A> flatten() {
   final F2<Tree<A>, P1<Stream<A>>, Stream<A>> squish =
       new F2<Tree<A>, P1<Stream<A>>, Stream<A>>() {
         public Stream<A> f(final Tree<A> t, final P1<Stream<A>> xs) {
           return cons(
               t.root(),
               t.subForest()
                   .map(
                       Stream.<Tree<A>, Stream<A>>foldRight()
                           .f(F2Functions.curry(this))
                           .f(xs._1())));
         }
       };
   return squish.f(this, P.p(Stream.nil()));
 }
Esempio n. 3
0
 /**
  * Creates a nullary tree.
  *
  * @param root The root element of the tree.
  * @return A nullary tree with the root element in it.
  */
 public static <A> Tree<A> leaf(final A root) {
   return node(root, Stream.nil());
 }
Esempio n. 4
0
 /**
  * Returns a single element stream if this projection has a value, otherwise an empty stream.
  *
  * @return A single element stream if this projection has a value, otherwise an empty stream.
  */
 public Stream<B> toStream() {
   return isRight() ? Stream.single(value()) : Stream.<B>nil();
 }
Esempio n. 5
0
 /**
  * Returns a single element stream if this projection has a value, otherwise an empty stream.
  *
  * @return A single element stream if this projection has a value, otherwise an empty stream.
  */
 public Stream<A> toStream() {
   return isLeft() ? Stream.single(value()) : Stream.<A>nil();
 }