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())); }
/** * Applies a stream of comonadic functions to this product, returning a stream of values. * * @param fs A stream of functions to apply to this product. * @return A stream of the results of applying the given stream of functions to this product. */ public <C> Stream<C> sequenceW(final Stream<F<P2<A, B>, C>> fs) { return fs.isEmpty() ? Stream.<C>nil() : Stream.cons( fs.head().f(this), new P1<Stream<C>>() { public Stream<C> _1() { return sequenceW(fs.tail()._1()); } }); }
/** * 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(curry()).f(xs._1()))); } }; return squish.f(this, P.p(Stream.<A>nil())); }
/** * 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()); }
/** * 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(); }
/** * 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(); }
/** * Returns a stream projection of this optional value. * * @return A stream projection of this optional value. */ public Stream<A> toStream() { return isSome() ? Stream.<A>nil().cons(some()) : Stream.<A>nil(); }