com.stripe.bonsai

TreeOps

trait TreeOps[Tree, Label] extends AnyRef

TreeOps is a type class for abstracting over arbitrary trees, allowing traversal and data access. In general, it assumes that we can quickly access the children and label of any given node.

There are some convenience implicit methods added onto trees and nodes that can be accessed by importing tree ops. For instance, say we wanted to add an implicit method to any type of tree that let's us find some node in a tree.

implicit class NodeFinder[T](tree: T)(implicit treeOps: TreeOps[T]) {

// Brings in Node/Label types, and implicit classes.
import treeOps._

// Returns the first node (DFS) where the predicate is true.
def find(f: Label => Boolean): Option[Node] = {
  def recur(node: Node): Option[Node] = {
    // treeOps.TreeNodeOps brings in the Node#label implicit method.
    if (f(node.label)) {
      node
    } else {
      // treeOps.TreeNodeOps also brings in Node#children implicit method.
      node.children.iterator.map(recur).find(_.isDefined).flatten
    }
  }

  // treeOps.TreeTreeOps brings in T#root.
  recur(tree.root)
}
}
Self Type
TreeOps[Tree, Label]
Linear Supertypes
Known Subclasses
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. TreeOps
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Type Members

  1. abstract type Node

    The type of the nodes in the tree.

  2. implicit class OpsForNode extends AnyRef

  3. implicit class OpsForTree extends AnyRef

Abstract Value Members

  1. abstract def children(node: Node): Iterable[Node]

    Returns all the direct children of the given node.

    Returns all the direct children of the given node. The order may or may not matter. TreeOps does not provide any guarantees here.

  2. abstract def label(node: Node): Label

    Returns the label attached to the given node.

  3. abstract def root(t: Tree): Option[Node]

    Returns the root node of the tree.

Concrete Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  7. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. def fold[A](tree: Tree)(f: (Label, Iterable[Node]) ⇒ A): Option[A]

  12. def fold[A](node: Node)(f: (Label, Iterable[Node]) ⇒ A): A

  13. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  14. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  15. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  16. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  17. final def notify(): Unit

    Definition Classes
    AnyRef
  18. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  19. def reduce[A](tree: Tree)(f: (Label, Iterable[A]) ⇒ A): Option[A]

  20. def reduce[A](node: Node)(f: (Label, Iterable[A]) ⇒ A): A

  21. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  22. def toString(): String

    Definition Classes
    AnyRef → Any
  23. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  24. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  25. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped