Package fj.data

Class TreeZipper<A>

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

    public final class TreeZipper<A>
    extends java.lang.Object
    implements java.lang.Iterable<TreeZipper<A>>
    Provides a zipper structure for rose trees, which is a Tree supplied with a location within that tree. Provides navigation, insertion, deletion, and memorization of visited locations within a tree.
    • Method Detail

      • iterator

        public java.util.Iterator<TreeZipper<A>> iterator()
        Returns an iterator of all the positions of this TreeZipper. Exists for use with the foreach syntax.
        Specified by:
        iterator in interface java.lang.Iterable<A>
        Returns:
        An iterator of all the positions of this TreeZipper.
      • equals

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

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

        public static <A> TreeZipper<A> treeZipper​(Tree<A> tree,
                                                   Stream<Tree<A>> lefts,
                                                   Stream<Tree<A>> rights,
                                                   Stream<P3<Stream<Tree<A>>,​A,​Stream<Tree<A>>>> parents)
        Creates a new tree zipper given a currently selected tree, a forest on the left, a forest on the right, and a stream of parent contexts.
        Parameters:
        tree - The currently selected tree.
        lefts - The selected tree's left siblings, closest first.
        rights - The selected tree's right siblings, closest first.
        parents - The parent of the selected tree, and the parent's siblings.
        Returns:
        A new zipper with the given tree selected, and the given forests on the left and right.
      • treeZipper

        public static <A> F<Tree<A>,​F<Stream<Tree<A>>,​F<Stream<Tree<A>>,​F<Stream<P3<Stream<Tree<A>>,​A,​Stream<Tree<A>>>>,​TreeZipper<A>>>>> treeZipper()
        First-class constructor for tree zippers.
        Returns:
        A function that returns a new tree zipper, given a selected tree, left and right siblings, and a parent context.
      • eq

        public static <A> Equal<TreeZipper<A>> eq​(Equal<A> e)
        An Equal instance for tree zippers.
        Parameters:
        e - An Equal instance for tree elements.
        Returns:
        An Equal instance for tree zippers.
      • show

        public static <A> Show<TreeZipper<A>> show​(Show<A> s)
        A Show instance for tree zippers.
        Parameters:
        s - A Show instance for tree elements.
        Returns:
        A Show instance for tree zippers.
      • parent

        public Option<TreeZipper<A>> parent()
        Navigates to the parent of the current location.
        Returns:
        A new tree zipper focused on the parent node of the current node, or none if the current node is the root node.
      • root

        public TreeZipper<A> root()
        Navigates to the top-most parent of the current location.
        Returns:
        A new tree zipper focused on the top-most parent of the current node.
      • root_

        public static <A> F<TreeZipper<A>,​TreeZipper<A>> root_()
        A first-class version of the root function.
        Returns:
        A function that returns a new tree-zipper focused on the root of the given tree zipper's tree.
      • left

        public Option<TreeZipper<A>> left()
        Navigates to the left sibling of the current location.
        Returns:
        A new tree zipper focused on the left sibling of the current node, or none if there are no siblings on the left.
      • right

        public Option<TreeZipper<A>> right()
        Navigates to the right sibling of the current location.
        Returns:
        A new tree zipper focused on the right sibling of the current node, or none if there are no siblings on the right.
      • firstChild

        public Option<TreeZipper<A>> firstChild()
        Navigtes to the first child of the current location.
        Returns:
        A new tree zipper focused on the first child of the current node, or none if the node has no children.
      • lastChild

        public Option<TreeZipper<A>> lastChild()
        Navigtes to the last child of the current location.
        Returns:
        A new tree zipper focused on the last child of the current node, or none if the node has no children.
      • getChild

        public Option<TreeZipper<A>> getChild​(int n)
        Navigates to the given child of the current location, starting at index 0.
        Parameters:
        n - The index of the child to which to navigate.
        Returns:
        An optional tree zipper focused on the child node at the given index, or none if there is no such child.
      • findChild

        public Option<TreeZipper<A>> findChild​(F<Tree<A>,​java.lang.Boolean> p)
        Navigates to the first child of the current location, that satisfies the given predicate.
        Parameters:
        p - A predicate to be satisfied by the child node.
        Returns:
        An optional tree zipper focused on the first child node that satisfies the given predicate, or none if there is no such child.
      • fromTree

        public static <A> TreeZipper<A> fromTree​(Tree<A> t)
        Creates a new tree zipper focused on the root of the given tree.
        Parameters:
        t - A tree over which to create a new zipper.
        Returns:
        a new tree zipper focused on the root of the given tree.
      • fromForest

        public static <A> Option<TreeZipper<A>> fromForest​(Stream<Tree<A>> ts)
        Creates a new tree zipper focused on the first element of the given forest.
        Parameters:
        ts - A forest over which to create a new zipper.
        Returns:
        a new tree zipper focused on the first element of the given forest.
      • toTree

        public Tree<A> toTree()
        Returns the tree containing this location.
        Returns:
        the tree containing this location.
      • toForest

        public Stream<Tree<A>> toForest()
        Returns the forest containing this location.
        Returns:
        the forest containing this location.
      • focus

        public Tree<A> focus()
        Returns the tree at the currently focused node.
        Returns:
        the tree at the currently focused node.
      • lefts

        public Stream<Tree<A>> lefts()
        Returns the left siblings of the currently focused node.
        Returns:
        the left siblings of the currently focused node.
      • rights

        public Stream<Tree<A>> rights()
        Returns the right siblings of the currently focused node.
        Returns:
        the right siblings of the currently focused node.
      • parents

        public Stream<P3<Stream<Tree<A>>,​A,​Stream<Tree<A>>>> parents()
        Returns the parents of the currently focused node.
        Returns:
        the parents of the currently focused node.
      • isRoot

        public boolean isRoot()
        Indicates whether the current node is at the top of the tree.
        Returns:
        true if the current node is the root of the tree, otherwise false.
      • isFirst

        public boolean isFirst()
        Indicates whether the current node is the leftmost tree in the current forest.
        Returns:
        true if the current node has no left siblings, otherwise false.
      • isLast

        public boolean isLast()
        Indicates whether the current node is the rightmost tree in the current forest.
        Returns:
        true if the current node has no siblings on its right, otherwise false.
      • isLeaf

        public boolean isLeaf()
        Indicates whether the current node is at the bottom of the tree.
        Returns:
        true if the current node has no child nodes, otherwise false.
      • isChild

        public boolean isChild()
        Indicates whether the current node is a child node of another node.
        Returns:
        true if the current node has a parent node, otherwise false.
      • hasChildren

        public boolean hasChildren()
        Indicates whether the current node has any child nodes.
        Returns:
        true if the current node has child nodes, otherwise false.
      • setTree

        public TreeZipper<A> setTree​(Tree<A> t)
        Replaces the current node with the given tree.
        Parameters:
        t - A tree with which to replace the current node.
        Returns:
        A new tree zipper in which the focused node is replaced with the given tree.
      • modifyTree

        public TreeZipper<A> modifyTree​(F<Tree<A>,​Tree<A>> f)
        Modifies the current node with the given function.
        Parameters:
        f - A function with which to modify the current tree.
        Returns:
        A new tree zipper in which the focused node has been transformed by the given function.
      • modifyLabel

        public TreeZipper<A> modifyLabel​(F<A,​A> f)
        Modifies the label at the current node with the given function.
        Parameters:
        f - A function with which to transform the current node's label.
        Returns:
        A new tree zipper with the focused node's label transformed by the given function.
      • setLabel

        public TreeZipper<A> setLabel​(A v)
        Replaces the label of the current node with the given value.
        Parameters:
        v - The new value for the node's label.
        Returns:
        A new tree zipper with the focused node's label replaced by the given value.
      • getLabel

        public A getLabel()
        Returns the label at the current node.
        Returns:
        the label at the current node.
      • insertLeft

        public TreeZipper<A> insertLeft​(Tree<A> t)
        Inserts a tree to the left of the current position. The inserted tree becomes the current tree.
        Parameters:
        t - A tree to insert to the left of the current position.
        Returns:
        A new tree zipper with the given tree in focus and the current tree on the right.
      • insertRight

        public TreeZipper<A> insertRight​(Tree<A> t)
        Inserts a tree to the right of the current position. The inserted tree becomes the current tree.
        Parameters:
        t - A tree to insert to the right of the current position.
        Returns:
        A new tree zipper with the given tree in focus and the current tree on the left.
      • insertDownFirst

        public TreeZipper<A> insertDownFirst​(Tree<A> t)
        Inserts a tree as the first child of the current node. The inserted tree becomes the current tree.
        Parameters:
        t - A tree to insert.
        Returns:
        A new tree zipper with the given tree in focus, as the first child of the current node.
      • insertDownLast

        public TreeZipper<A> insertDownLast​(Tree<A> t)
        Inserts a tree as the last child of the current node. The inserted tree becomes the current tree.
        Parameters:
        t - A tree to insert.
        Returns:
        A new tree zipper with the given tree in focus, as the last child of the current node.
      • insertDownAt

        public Option<TreeZipper<A>> insertDownAt​(int n,
                                                  Tree<A> t)
        Inserts a tree at the specified location in the current node's stream of children. The inserted tree becomes the current node.
        Parameters:
        n - The index at which to insert the given tree, starting at 0.
        t - A tree to insert.
        Returns:
        A new tree zipper with the given tree in focus, at the specified index in the current node's stream of children, or None if the current node has fewer than n children.
      • delete

        public Option<TreeZipper<A>> delete()
        Removes the current node from the tree. The new position becomes the right sibling, or the left sibling if the current node has no right siblings, or the parent node if the current node has no siblings.
        Returns:
        A new tree zipper with the current node removed.
      • zipWithFocus

        public TreeZipper<P2<A,​java.lang.Boolean>> zipWithFocus()
        Zips the nodes in this zipper with a boolean that indicates whether that node has focus. All of the booleans will be false, except for the focused node.
        Returns:
        A new zipper of pairs, with each node of this zipper paired with a boolean that is true if that node has focus, and false otherwise.
      • map

        public <B> TreeZipper<B> map​(F<A,​B> f)
        Maps the given function across this zipper (covariant functor pattern).
        Parameters:
        f - A function to map across this zipper.
        Returns:
        A new zipper with the given function applied to the label of every node.
      • fromTree

        public static <A> F<Tree<A>,​TreeZipper<A>> fromTree()
        First-class conversion of a Tree to the corresponding tree zipper.
        Returns:
        A function that takes a tree to its tree zipper representation.
      • left_

        public static <A> F<TreeZipper<A>,​Option<TreeZipper<A>>> left_()
        A first-class version of the left() function.
        Returns:
        A function that focuses the given tree zipper on its left sibling.
      • right_

        public static <A> F<TreeZipper<A>,​Option<TreeZipper<A>>> right_()
        A first-class version of the right() function.
        Returns:
        A function that focuses the given tree zipper on its right sibling.
      • positions

        public TreeZipper<TreeZipper<A>> positions()
        Returns a zipper over the tree of all possible permutations of this tree zipper (comonad pattern). This tree zipper becomes the focused node of the new zipper.
        Returns:
        A tree zipper over the tree of all possible permutations of this tree zipper.
      • cobind

        public <B> TreeZipper<B> cobind​(F<TreeZipper<A>,​B> f)
        Maps the given function over the tree of all positions for this zipper (comonad pattern). Returns a zipper over the tree of results of the function application.
        Parameters:
        f - A function to map over the tree of all positions for this zipper.
        Returns:
        A zipper over the tree of results of the function application.
      • findChild

        public static <A> F2<F<Tree<A>,​java.lang.Boolean>,​TreeZipper<A>,​Option<TreeZipper<A>>> findChild()
        A first-class version of the findChild function.
        Returns:
        a function that finds the first child, of a given tree zipper, that matches a given predicate.
      • zipWith

        public <B,​C> TreeZipper<C> zipWith​(TreeZipper<B> bs,
                                                 F2<A,​B,​C> f)
        Zips this TreeZipper with another, applying the given function lock-step over both zippers in all directions. The structure of the resulting TreeZipper is the structural intersection of the two TreeZippers.
        Parameters:
        bs - A TreeZipper to zip this one with.
        f - A function with which to zip together the two TreeZippers.
        Returns:
        The result of applying the given function over this TreeZipper and the given TreeZipper, location-wise.
      • zipWith

        public <B,​C> TreeZipper<C> zipWith​(TreeZipper<B> bs,
                                                 F<A,​F<B,​C>> f)
        Zips this TreeZipper with another, applying the given function lock-step over both zippers in all directions. The structure of the resulting TreeZipper is the structural intersection of the two TreeZippers.
        Parameters:
        bs - A TreeZipper to zip this one with.
        f - A function with which to zip together the two TreeZippers.
        Returns:
        The result of applying the given function over this TreeZipper and the given TreeZipper, location-wise.