Package fj.data
Class TreeZipper<A>
java.lang.Object
fj.data.TreeZipper<A>
- All Implemented Interfaces:
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.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<B> TreeZipper
<B> cobind
(F<TreeZipper<A>, B> f) Maps the given function over the tree of all positions for this zipper (comonad pattern).delete()
Removes the current node from the tree.private static <A> F
<TreeZipper<A>, P2<TreeZipper<A>, P1<Stream<TreeZipper<A>>>>> dwn()
static <A> Equal
<TreeZipper<A>> An Equal instance for tree zippers.final boolean
static <A> F2
<F<Tree<A>, Boolean>, TreeZipper<A>, Option<TreeZipper<A>>> A first-class version of the findChild function.Navigates to the first child of the current location, that satisfies the given predicate.Navigtes to the first child of the current location.focus()
Returns the tree at the currently focused node.static <A> Option
<TreeZipper<A>> fromForest
(Stream<Tree<A>> ts) Creates a new tree zipper focused on the first element of the given forest.static <A> F
<Tree<A>, TreeZipper<A>> fromTree()
First-class conversion of a Tree to the corresponding tree zipper.static <A> TreeZipper
<A> Creates a new tree zipper focused on the root of the given tree.getChild
(int n) Navigates to the given child of the current location, starting at index 0.getLabel()
Returns the label at the current node.boolean
Indicates whether the current node has any child nodes.final int
hashCode()
insertDownAt
(int n, Tree<A> t) Inserts a tree at the specified location in the current node's stream of children.insertDownFirst
(Tree<A> t) Inserts a tree as the first child of the current node.insertDownLast
(Tree<A> t) Inserts a tree as the last child of the current node.insertLeft
(Tree<A> t) Inserts a tree to the left of the current position.insertRight
(Tree<A> t) Inserts a tree to the right of the current position.boolean
isChild()
Indicates whether the current node is a child node of another node.boolean
isFirst()
Indicates whether the current node is the leftmost tree in the current forest.boolean
isLast()
Indicates whether the current node is the rightmost tree in the current forest.boolean
isLeaf()
Indicates whether the current node is at the bottom of the tree.boolean
isRoot()
Indicates whether the current node is at the top of the tree.iterator()
Returns an iterator of all the positions of this TreeZipper.Navigtes to the last child of the current location.left()
Navigates to the left sibling of the current location.static <A> F
<TreeZipper<A>, Option<TreeZipper<A>>> left_()
A first-class version of the left() function.lefts()
Returns the left siblings of the currently focused node.lp3nil()
<B> TreeZipper
<B> Maps the given function across this zipper (covariant functor pattern).modifyLabel
(F<A, A> f) Modifies the label at the current node with the given function.Modifies the current node with the given function.p()
Returns the product-4 representation of this zipper.static <A> F
<TreeZipper<A>, P4<Tree<A>, Stream<Tree<A>>, Stream<Tree<A>>, Stream<P3<Stream<Tree<A>>, A, Stream<Tree<A>>>>>> p_()
A first-class function that returns the product-4 representation of a given zipper.parent()
Navigates to the parent of the current location.parents()
Returns the parents of the currently focused node.Returns a zipper over the tree of all possible permutations of this tree zipper (comonad pattern).right()
Navigates to the right sibling of the current location.static <A> F
<TreeZipper<A>, Option<TreeZipper<A>>> right_()
A first-class version of the right() function.rights()
Returns the right siblings of the currently focused node.root()
Navigates to the top-most parent of the current location.static <A> F
<TreeZipper<A>, TreeZipper<A>> root_()
A first-class version of the root function.Replaces the label of the current node with the given value.Replaces the current node with the given tree.static <A> Show
<TreeZipper<A>> A Show instance for tree zippers.splitChildren
(Stream<A> acc, Stream<A> xs, int n) toForest()
Returns the forest containing this location.toTree()
Returns the tree containing this location.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>>>>> First-class constructor for tree zippers.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.private Stream
<Tree<TreeZipper<A>>> uf
(F<TreeZipper<A>, Option<TreeZipper<A>>> f) <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.<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.TreeZipper
<P2<A, Boolean>> Zips the nodes in this zipper with a boolean that indicates whether that node has focus.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
tree
-
lefts
-
rights
-
parents
-
-
Constructor Details
-
TreeZipper
-
-
Method Details
-
iterator
Returns an iterator of all the positions of this TreeZipper. Exists for use with the foreach syntax. -
equals
-
hashCode
public final int hashCode() -
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>>, treeZipper()F<Stream<Tree<A>>, F<Stream<P3<Stream<Tree<A>>, A, Stream<Tree<A>>>>, TreeZipper<A>>>>> 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.
-
p
public P4<Tree<A>,Stream<Tree<A>>, p()Stream<Tree<A>>, Stream<P3<Stream<Tree<A>>, A, Stream<Tree<A>>>>> Returns the product-4 representation of this zipper.- Returns:
- the product-4 representation of this zipper.
-
p_
public static <A> F<TreeZipper<A>,P4<Tree<A>, p_()Stream<Tree<A>>, Stream<Tree<A>>, Stream<P3<Stream<Tree<A>>, A, Stream<Tree<A>>>>>> A first-class function that returns the product-4 representation of a given zipper.- Returns:
- a function that converts a given zipper to its product-4 representation.
-
eq
An Equal instance for tree zippers.- Parameters:
e
- An Equal instance for tree elements.- Returns:
- An Equal instance for tree zippers.
-
show
A Show instance for tree zippers.- Parameters:
s
- A Show instance for tree elements.- Returns:
- A Show instance for tree zippers.
-
combChildren
-
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
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_
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
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
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
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
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
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
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.
-
downParents
-
splitChildren
-
lp3nil
-
fromTree
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
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
Returns the tree containing this location.- Returns:
- the tree containing this location.
-
toForest
Returns the forest containing this location.- Returns:
- the forest containing this location.
-
focus
Returns the tree at the currently focused node.- Returns:
- the tree at the currently focused node.
-
lefts
Returns the left siblings of the currently focused node.- Returns:
- the left siblings of the currently focused node.
-
rights
Returns the right siblings of the currently focused node.- Returns:
- the right siblings of the currently focused node.
-
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
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
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
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
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
Returns the label at the current node.- Returns:
- the label at the current node.
-
insertLeft
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
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
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
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
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
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
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
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
First-class conversion of a Tree to the corresponding tree zipper.- Returns:
- A function that takes a tree to its tree zipper representation.
-
left_
A first-class version of the left() function.- Returns:
- A function that focuses the given tree zipper on its left sibling.
-
right_
A first-class version of the right() function.- Returns:
- A function that focuses the given tree zipper on its right sibling.
-
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.
-
uf
-
dwn
-
cobind
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
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
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
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.
-