Created
September 16, 2013 15:56
-
-
Save wheaties/6582588 to your computer and use it in GitHub Desktop.
Some Tree Ideas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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