Skip to content

Instantly share code, notes, and snippets.

@wheaties
Created September 16, 2013 15:56
Show Gist options
  • Save wheaties/6582588 to your computer and use it in GitHub Desktop.
Save wheaties/6582588 to your computer and use it in GitHub Desktop.
Some Tree Ideas
//Haven't even checked if this compiles
//Just an idea
trait TreeLike[A, This <: IterableLike[TreeLike[A, This], This]]{
def root: A
def children: This
def map[B, That](f: A => B): TreeLike[A, That] = over(this, f).run
protected def over[B, That](tree: TreeLike[A, This], f: A => B): Trampoline[TreeLike[B, That]] =
if(tree.children.isEmpty) done(build(f(tree.root))
else for{
childs <- step(tree.children, f)
} yield build(f(tree.root), childs)
protected def step[B, That](forest: This, f: A => B, acc: That): Trampoline[That] =
if(forest.isEmpty) done(acc)
else for{
item <- suspend(over(forest.head, f))
remainder <- suspend(step(forest.tail, f, acc))
} yield remainder :+ item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment