Skip to content

Instantly share code, notes, and snippets.

@wheaties
Last active December 23, 2015 09:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wheaties/6613325 to your computer and use it in GitHub Desktop.
Save wheaties/6613325 to your computer and use it in GitHub Desktop.
Scalaz Trampoline 7.0.2 on 2.10.2 fail...
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))
}
}
}
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)
}
}
@xuwei-k
Copy link

xuwei-k commented Feb 3, 2014

Maybe scalac bug. will be fixed in Scala2.11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment