Created
May 18, 2014 09:52
-
-
Save adilakhter/64a2bc93f3b440fbef88 to your computer and use it in GitHub Desktop.
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
def even2(n: Int): Bounce[Boolean] = { | |
if (n == 0) Done(true) | |
else Call(() => odd2(n - 1)) | |
} | |
def odd2(n: Int): Bounce[Boolean] = { | |
if (n == 0) Done(false) | |
else Call(() => even2(n - 1)) | |
} | |
sealed trait Bounce[A] | |
case class Done[A](result: A) extends Bounce[A] | |
case class Call[A](thunk: () => Bounce[A]) extends Bounce[A] | |
def trampoline[A](bounce: Bounce[A]): A = bounce match { | |
case Call(thunk) => trampoline(thunk()) | |
case Done(x) => x | |
} | |
trampoline(even2(9999)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment