Skip to content

Instantly share code, notes, and snippets.

@Rogach
Created January 23, 2012 08:18
Show Gist options
  • Save Rogach/1661713 to your computer and use it in GitHub Desktop.
Save Rogach/1661713 to your computer and use it in GitHub Desktop.
Non-homogeneous tree
package socco
abstract class SoccoTree(val children:List[SoccoTree]) extends Seq[SoccoTree] with SoccoNode[SoccoTree] {
def apply(n:Int) = children(n)
def iterator = children.iterator
def length = children.length
// def add(t:SoccoTree)(implicit builder:SoccoTreeBuilder[B]) = builder.build(this.asInstanceOf[B], children :+ t)
}
trait SoccoNode[B <: SoccoTree] {
def soccoCompanion:SoccoTreeCompanion[B]
}
trait SoccoTreeCompanion[A <: SoccoTree] {
implicit val builder:SoccoTreeBuilder[A]
}
trait SoccoTreeBuilder[A <: SoccoTree] {
def build(node:A, children:List[SoccoTree]):A
}
object Directory extends SoccoTreeCompanion[Directory] {
def apply(name:String) = new Directory(name,List())
implicit val builder = new SoccoTreeBuilder[Directory] {
def build(node:Directory, children:List[SoccoTree]) = new Directory(node.name, children)
}
}
class Directory(val name:String,
val _children:List[SoccoTree]) extends SoccoTree(_children) with SoccoNode[Directory] {
def soccoCompanion = Directory
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment