public final class Tree<A>
extends java.lang.Object
implements java.lang.Iterable<A>
Modifier and Type | Method and 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.
|
boolean |
equals(java.lang.Object other) |
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.
|
Stream<A> |
flatten()
Puts the elements of the tree into a Stream, in pre-order.
|
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> Tree<B> |
fmap(F<A,B> f)
Maps the given function over this tree.
|
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.
|
<B> B |
foldMap(F<A,B> f,
Monoid<B> m)
Folds this tree using the given monoid.
|
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,
P1<Stream<Tree<A>>> forest)
Creates a new tree given a root and a (potentially infinite) subforest.
|
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> F<Tree<A>,A> |
root_()
Provides a transformation from a tree to its root.
|
A |
root()
Returns the root element of the tree.
|
static <A> Show<Tree<A>> |
show2D(Show<A> s)
Provides a show instance that draws a 2-dimensional representation of a tree.
|
static <A> F<Tree<A>,P1<Stream<Tree<A>>>> |
subForest_()
Provides a transformation from a tree to its subforest.
|
P1<Stream<Tree<A>>> |
subForest()
Returns a stream of the tree's subtrees.
|
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.
|
public java.util.Iterator<A> iterator()
for
-each loop.iterator
in interface java.lang.Iterable<A>
public static <A> Tree<A> leaf(A root)
root
- The root element of the tree.public static <A> Tree<A> node(A root, P1<Stream<Tree<A>>> forest)
root
- The root element of the tree.forest
- A stream of the tree's subtrees.public static <A> Tree<A> node(A root, Stream<Tree<A>> forest)
root
- The root element of the tree.forest
- A stream of the tree's subtrees.public static <A> Tree<A> node(A root, List<Tree<A>> forest)
root
- The root element of the tree.forest
- A list of the tree's subtrees.public static <A> F<A,F<P1<Stream<Tree<A>>>,Tree<A>>> node()
public A root()
public P1<Stream<Tree<A>>> subForest()
public static <A> F<Tree<A>,A> root_()
public static <A> F<Tree<A>,P1<Stream<Tree<A>>>> subForest_()
public Stream<A> flatten()
public static <A> F<Tree<A>,Stream<A>> flatten_()
public Stream<Stream<A>> levels()
public <B> Tree<B> fmap(F<A,B> f)
f
- The function to map over this tree.public static <A,B> F<F<A,B>,F<Tree<A>,Tree<B>>> fmap_()
public <B> B foldMap(F<A,B> f, Monoid<B> m)
f
- A transformation from this tree's elements, to the monoid.m
- The monoid to fold this tree with.public java.util.Collection<A> toCollection()
public static <A,B> F<Tree<A>,B> foldMap_(F<A,B> f, Monoid<B> m)
f
- A transformation from a tree's elements to the monoid.m
- A monoid to fold the tree with.public static <A,B> F<B,Tree<A>> unfoldTree(F<B,P2<A,P1<Stream<B>>>> f)
f
- A function with which to build the tree.public <B> Tree<B> cobind(F<Tree<A>,B> f)
f
- A function to bind across all the subtrees of this tree.public Tree<Tree<A>> cojoin()
public boolean equals(java.lang.Object other)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String draw(Show<A> s)
s
- A show instance for the elements of the tree.public static <A> Show<Tree<A>> show2D(Show<A> s)
s
- A show instance for the elements of the tree.public <B,C> Tree<C> zipWith(Tree<B> bs, F2<A,B,C> f)
bs
- A tree to zip this tree with.f
- A function with which to zip together the two trees.public <B,C> Tree<C> zipWith(Tree<B> bs, F<A,F<B,C>> f)
bs
- A tree to zip this tree with.f
- A function with which to zip together the two trees.public static <A,B> Tree<B> bottomUp(Tree<A> t, F<P2<A,Stream<B>>,B> f)
t
- A tree to fold from the bottom to the top.f
- A function transforming the current node and a stream of already transformed nodes (its children) into a new nodepublic boolean isLeaf()
public int length()