Skip to content

Instantly share code, notes, and snippets.

@tobyweston
Created July 17, 2013 09:55
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 tobyweston/6019269 to your computer and use it in GitHub Desktop.
Save tobyweston/6019269 to your computer and use it in GitHub Desktop.
Tail recursion in Sacala
import org.scalatest.FunSuite
class RecursionTest extends FunSuite {
case class Explosion(message: String) extends RuntimeException
object Bomb {
def tick(countdown: Int): Int = {
println("hello")
if (countdown == 0) throw new Explosion("BOOOOM!")
else tick(countdown - 1)
}
}
test("recursion") {
Bomb tick(3)
}
}
@tobyweston
Copy link
Author

import scala.annotation.tailrec

class RecursionTest {
  @tailrec final def tick(countdown: Int): Int = {
    if (countdown == 0) throw new Exception("BOOOOM!")
    else tick(countdown - 1)
  }
}

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