Last active
December 23, 2015 09:19
-
-
Save wheaties/6613325 to your computer and use it in GitHub Desktop.
Scalaz Trampoline 7.0.2 on 2.10.2 fail...
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
class CTree extends Specification{ | |
"map" should { | |
"work with a tree without roots" in { //Works! | |
val blah = CTree(1) | |
val yo = blah map (_ + 1) | |
yo.root must beEqualTo(2) | |
} | |
"work with nested trees" in { //Fails! java.lang.AbstractMethodError... | |
val blah = CTree(1, Map(1 -> CTree(1))) | |
val yo = blah map (_ + 1) | |
(yo.root must beEqualTo(2)) and | |
(yo.children(1).root must beEqualTo(2)) | |
} | |
} | |
} |
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
case class CTree[@specialized(Double) Value](root: Value, | |
children: Map[Int, ComputedTree[Value]] = Map.empty[Int, CTree[Value]]){ | |
def map[A](f: Value => A): ComputedTree[A] = walk(this, f).run | |
protected def walk[A](tree: CTree[Value], f: Value => A): Trampoline[CTree[A]] = tree match { | |
case CTree(value, childs) if childs.isEmpty => done(CTree(f(value))) | |
case CTree(value, childs) => step(childs.toList, f) map (CTree(f(value), _)) | |
} | |
protected def step[A](trees: List[(Int, CTree[Value])], | |
f: Value => A, | |
acc: List[(Int, CTree[A])] = Nil): Trampoline[Map[Int, CTree[A]]] = trees match { | |
case (id, head) :: tail => for { | |
that <- suspend(walk(head, f)) | |
remainder <- suspend(step(tail, f, (id, that) :: acc)) | |
} yield remainder | |
case Nil => done(acc.toMap) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maybe scalac bug. will be fixed in Scala2.11