Package fj.data

Class Tree<A>

  • All Implemented Interfaces:
    java.lang.Iterable<A>

    public final class Tree<A>
    extends java.lang.Object
    implements java.lang.Iterable<A>
    Provides a lazy, immutable, non-empty, multi-way tree (a rose tree).
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Tree​(A root, P1<Stream<Tree<A>>> subForest)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <A,​B>
      Tree<B>
      bottomUp​(Tree<A> t, F<P2<A,​Stream<B>>,​B> f)
      <B> Tree<B> cobind​(F<Tree<A>,​B> f)
      Applies the given function to all subtrees of this tree, returning a tree of the results (comonad pattern).
      Tree<Tree<A>> cojoin()
      Expands this tree into a tree of trees, with this tree as the root label, and subtrees as the labels of child nodes (comonad pattern).
      java.lang.String draw​(Show<A> s)
      Draws a 2-dimensional representation of a tree.
      private static <A> Stream<java.lang.String> drawSubTrees​(Show<A> s, Stream<Tree<A>> ts)  
      private Stream<java.lang.String> drawTree​(Show<A> s)  
      boolean equals​(java.lang.Object other)  
      Stream<A> flatten()
      Puts the elements of the tree into a Stream, in pre-order.
      static <A> F<Tree<A>,​Stream<A>> flatten_()
      flatten :: Tree a -> [a] flatten t = squish t []
      <B> Tree<B> fmap​(F<A,​B> f)
      Maps the given function over this tree.
      static <A,​B>
      F<F<A,​B>,​F<Tree<A>,​Tree<B>>>
      fmap_()
      Provides a transformation to lift any function so that it maps over Trees.
      <B> B foldMap​(F<A,​B> f, Monoid<B> m)
      Folds this tree using the given monoid.
      static <A,​B>
      F<Tree<A>,​B>
      foldMap_​(F<A,​B> f, Monoid<B> m)
      Provides a function that folds a tree with the given monoid.
      private static <A> F<Tree<A>,​A> getRoot()  
      int hashCode()  
      boolean isLeaf()  
      java.util.Iterator<A> iterator()
      Returns an iterator for this tree.
      static <A> Tree<A> leaf​(A root)
      Creates a nullary tree.
      int length()  
      Stream<Stream<A>> levels()
      Provides a stream of the elements of the tree at each level, in level order.
      static <A> F<A,​F<P1<Stream<Tree<A>>>,​Tree<A>>> node()
      First-class constructor of trees.
      static <A> Tree<A> node​(A root, List<Tree<A>> forest)
      Creates a new n-ary given a root and a subforest of length n.
      static <A> Tree<A> node​(A root, Stream<Tree<A>> forest)
      Creates a new tree given a root and a (potentially infinite) subforest.
      static <A> Tree<A> node​(A root, P1<Stream<Tree<A>>> forest)
      Creates a new tree given a root and a (potentially infinite) subforest.
      A root()
      Returns the root element of the tree.
      static <A> F<Tree<A>,​A> root_()
      Provides a transformation from a tree to its root.
      private static Stream<java.lang.String> shift​(java.lang.String f, java.lang.String o, Stream<java.lang.String> s)  
      static <A> Show<Tree<A>> show2D​(Show<A> s)
      Provides a show instance that draws a 2-dimensional representation of a tree.
      P1<Stream<Tree<A>>> subForest()
      Returns a stream of the tree's subtrees.
      static <A> F<Tree<A>,​P1<Stream<Tree<A>>>> subForest_()
      Provides a transformation from a tree to its subforest.
      java.util.Collection<A> toCollection()
      Projects an immutable collection of this tree.
      java.lang.String toString()  
      static <A,​B>
      F<B,​Tree<A>>
      unfoldTree​(F<B,​P2<A,​P1<Stream<B>>>> f)
      Builds a tree from a seed value.
      <B,​C>
      Tree<C>
      zipWith​(Tree<B> bs, F<A,​F<B,​C>> f)
      Zips this tree with another, using the given function.
      <B,​C>
      Tree<C>
      zipWith​(Tree<B> bs, F2<A,​B,​C> f)
      Zips this tree with another, using the given function.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Field Detail

      • root

        private final A root
    • Method Detail

      • iterator

        public java.util.Iterator<A> iterator()
        Returns an iterator for this tree. This method exists to permit the use in a for-each loop.
        Specified by:
        iterator in interface java.lang.Iterable<A>
        Returns:
        A iterator for this tree.
      • leaf

        public static <A> Tree<A> leaf​(A root)
        Creates a nullary tree.
        Parameters:
        root - The root element of the tree.
        Returns:
        A nullary tree with the root element in it.
      • node

        public static <A> Tree<A> node​(A root,
                                       P1<Stream<Tree<A>>> forest)
        Creates a new tree given a root and a (potentially infinite) subforest.
        Parameters:
        root - The root element of the tree.
        forest - A stream of the tree's subtrees.
        Returns:
        A newly sprouted tree.
      • node

        public static <A> Tree<A> node​(A root,
                                       Stream<Tree<A>> forest)
        Creates a new tree given a root and a (potentially infinite) subforest.
        Parameters:
        root - The root element of the tree.
        forest - A stream of the tree's subtrees.
        Returns:
        A newly sprouted tree.
      • node

        public static <A> Tree<A> node​(A root,
                                       List<Tree<A>> forest)
        Creates a new n-ary given a root and a subforest of length n.
        Parameters:
        root - The root element of the tree.
        forest - A list of the tree's subtrees.
        Returns:
        A newly sprouted tree.
      • node

        public static <A> F<A,​F<P1<Stream<Tree<A>>>,​Tree<A>>> node()
        First-class constructor of trees.
        Returns:
        A function that constructs an n-ary tree given a root and a subforest or length n.
      • root

        public A root()
        Returns the root element of the tree.
        Returns:
        The root element of the tree.
      • subForest

        public P1<Stream<Tree<A>>> subForest()
        Returns a stream of the tree's subtrees.
        Returns:
        A stream of the tree's subtrees.
      • root_

        public static <A> F<Tree<A>,​A> root_()
        Provides a transformation from a tree to its root.
        Returns:
        A transformation from a tree to its root.
      • subForest_

        public static <A> F<Tree<A>,​P1<Stream<Tree<A>>>> subForest_()
        Provides a transformation from a tree to its subforest.
        Returns:
        A transformation from a tree to its subforest.
      • flatten

        public Stream<A> flatten()
        Puts the elements of the tree into a Stream, in pre-order.
        Returns:
        The elements of the tree in pre-order.
      • flatten_

        public static <A> F<Tree<A>,​Stream<A>> flatten_()
        
         flatten :: Tree a -> [a]
         flatten t = squish t []
         
        where squish (Node x ts) xs = x:Prelude.foldr squish xs ts Puts the elements of the tree into a Stream, in pre-order.
        Returns:
        The elements of the tree in pre-order.
      • levels

        public Stream<Stream<A>> levels()
        Provides a stream of the elements of the tree at each level, in level order.
        Returns:
        The elements of the tree at each level.
      • fmap

        public <B> Tree<B> fmap​(F<A,​B> f)
        Maps the given function over this tree.
        Parameters:
        f - The function to map over this tree.
        Returns:
        The new Tree after the function has been applied to each element in this Tree.
      • fmap_

        public static <A,​B> F<F<A,​B>,​F<Tree<A>,​Tree<B>>> fmap_()
        Provides a transformation to lift any function so that it maps over Trees.
        Returns:
        A transformation to lift any function so that it maps over Trees.
      • foldMap

        public <B> B foldMap​(F<A,​B> f,
                             Monoid<B> m)
        Folds this tree using the given monoid.
        Parameters:
        f - A transformation from this tree's elements, to the monoid.
        m - The monoid to fold this tree with.
        Returns:
        The result of folding the tree with the given monoid.
      • toCollection

        public java.util.Collection<A> toCollection()
        Projects an immutable collection of this tree.
        Returns:
        An immutable collection of this tree.
      • foldMap_

        public static <A,​B> F<Tree<A>,​B> foldMap_​(F<A,​B> f,
                                                              Monoid<B> m)
        Provides a function that folds a tree with the given monoid.
        Parameters:
        f - A transformation from a tree's elements to the monoid.
        m - A monoid to fold the tree with.
        Returns:
        A function that, given a tree, folds it with the given monoid.
      • unfoldTree

        public static <A,​B> F<B,​Tree<A>> unfoldTree​(F<B,​P2<A,​P1<Stream<B>>>> f)
        Builds a tree from a seed value.
        Parameters:
        f - A function with which to build the tree.
        Returns:
        A function which, given a seed value, yields a tree.
      • cobind

        public <B> Tree<B> cobind​(F<Tree<A>,​B> f)
        Applies the given function to all subtrees of this tree, returning a tree of the results (comonad pattern).
        Parameters:
        f - A function to bind across all the subtrees of this tree.
        Returns:
        A new tree, with the results of applying the given function to each subtree of this tree. The result of applying the function to the entire tree is the root label, and the results of applying to the root's children are labels of the root's subforest, etc.
      • cojoin

        public Tree<Tree<A>> cojoin()
        Expands this tree into a tree of trees, with this tree as the root label, and subtrees as the labels of child nodes (comonad pattern).
        Returns:
        A tree of trees, with this tree as its root label, and subtrees of this tree as the labels of its child nodes.
      • drawSubTrees

        private static <A> Stream<java.lang.String> drawSubTrees​(Show<A> s,
                                                                 Stream<Tree<A>> ts)
      • shift

        private static Stream<java.lang.String> shift​(java.lang.String f,
                                                      java.lang.String o,
                                                      Stream<java.lang.String> s)
      • drawTree

        private Stream<java.lang.String> drawTree​(Show<A> s)
      • equals

        public boolean equals​(java.lang.Object other)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • draw

        public java.lang.String draw​(Show<A> s)
        Draws a 2-dimensional representation of a tree.
        Parameters:
        s - A show instance for the elements of the tree.
        Returns:
        a String showing this tree in two dimensions.
      • show2D

        public static <A> Show<Tree<A>> show2D​(Show<A> s)
        Provides a show instance that draws a 2-dimensional representation of a tree.
        Parameters:
        s - A show instance for the elements of the tree.
        Returns:
        a show instance that draws a 2-dimensional representation of a tree.
      • zipWith

        public <B,​C> Tree<C> zipWith​(Tree<B> bs,
                                           F2<A,​B,​C> f)
        Zips this tree with another, using the given function. The resulting tree is the structural intersection of the two trees.
        Parameters:
        bs - A tree to zip this tree with.
        f - A function with which to zip together the two trees.
        Returns:
        A new tree of the results of applying the given function over this tree and the given tree, position-wise.
      • zipWith

        public <B,​C> Tree<C> zipWith​(Tree<B> bs,
                                           F<A,​F<B,​C>> f)
        Zips this tree with another, using the given function. The resulting tree is the structural intersection of the two trees.
        Parameters:
        bs - A tree to zip this tree with.
        f - A function with which to zip together the two trees.
        Returns:
        A new tree of the results of applying the given function over this tree and the given tree, position-wise.
      • getRoot

        private static <A> F<Tree<A>,​A> getRoot()
        Returns:
        a function getting the root of a Tree
      • isLeaf

        public boolean isLeaf()
      • length

        public int length()