Skip to content

Instantly share code, notes, and snippets.

@adilakhter
Created May 18, 2014 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adilakhter/64a2bc93f3b440fbef88 to your computer and use it in GitHub Desktop.
Save adilakhter/64a2bc93f3b440fbef88 to your computer and use it in GitHub Desktop.
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