diff options
author | Martin Odersky <odersky@gmail.com> | 2008-12-09 17:40:50 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2008-12-09 17:40:50 +0000 |
commit | f83d8977544ec7fc3eed59e032e3705f30290c00 (patch) | |
tree | 2109641f8f4d84630726637ed480e0512c99e1d5 /src/library/scalax/util/control/TailRec.scala | |
parent | 4d32e17513cf46b786eef8523653ac366c73a09c (diff) | |
download | scala-f83d8977544ec7fc3eed59e032e3705f30290c00.tar.gz scala-f83d8977544ec7fc3eed59e032e3705f30290c00.tar.bz2 scala-f83d8977544ec7fc3eed59e032e3705f30290c00.zip |
updates to scalax collections and standard libr...
updates to scalax collections and standard library classes.
Diffstat (limited to 'src/library/scalax/util/control/TailRec.scala')
-rw-r--r-- | src/library/scalax/util/control/TailRec.scala | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/library/scalax/util/control/TailRec.scala b/src/library/scalax/util/control/TailRec.scala new file mode 100644 index 0000000000..db6cbfa2ed --- /dev/null +++ b/src/library/scalax/util/control/TailRec.scala @@ -0,0 +1,24 @@ +package scala.util.control + +abstract class TailRec[+A] + +object TailRec { + + case class Call[A](rest: () => TailRec[A]) extends TailRec[A] + case class Done[A](result: A) extends TailRec[A] + + def tailcall[A](rest: => TailRec[A]) = new Call(() => rest) + def done [A](result: A) = new Done(result) + def trampoline[A](body: TailRec[A]): A = { + def loop(body: TailRec[A]): A = body match { + case Call(rest) => loop(rest()) + case Done(result) => result + } + loop(body) + } + def loop[A](body: TailRec[A]): A = body match { + case Call(rest) => loop[A](rest()) + case Done(result) => result + } +} + |