/** * Provides a stream of the elements of the tree at each level, in level order. * * @return The elements of the tree at each level. */ public Stream<Stream<A>> levels() { final F<Stream<Tree<A>>, Stream<Tree<A>>> flatSubForests = Stream.<Tree<A>, Tree<A>>bind_() .f(compose(P1.<Stream<Tree<A>>>__1(), Tree.<A>subForest_())); final F<Stream<Tree<A>>, Stream<A>> roots = Stream.<Tree<A>, A>map_().f(Tree.<A>root_()); return iterateWhile(flatSubForests, Stream.<Tree<A>>isNotEmpty_(), single(this)).map(roots); }
/** * Builds a tree from a seed value. * * @param f A function with which to build the tree. * @return A function which, given a seed value, yields a tree. */ public static <A, B> F<B, Tree<A>> unfoldTree(final F<B, P2<A, P1<Stream<B>>>> f) { return b -> { final P2<A, P1<Stream<B>>> p = f.f(b); return node(p._1(), p._2().map(Stream.<B, Tree<A>>map_().f(unfoldTree(f)))); }; }
/** * Maps the given function over this tree. * * @param f The function to map over this tree. * @return The new Tree after the function has been applied to each element in this Tree. */ public <B> Tree<B> fmap(final F<A, B> f) { return node( f.f(root()), subForest().map(Stream.<Tree<A>, Tree<B>>map_().f(Tree.<A, B>fmap_().f(f)))); }