Skip to content

Instantly share code, notes, and snippets.

@tkellogg
Created June 23, 2013 19:43
Show Gist options
  • Save tkellogg/5846273 to your computer and use it in GitHub Desktop.
Save tkellogg/5846273 to your computer and use it in GitHub Desktop.
How extracting a method can make a function go from tail recursive to just regular recursive. There's a few more ways too.
def bar(value: Int) = foo(Some(value / 3))
def foo(value: Option[Int]): Option[Int] = value match {
case Some(v) if (v % 2 == 0) => bar(v)
case Some(v) => Some(v)
case None => None
}
def foo(value: Option[Int]): Option[Int] = value match {
case Some(v) if (v % 2 == 0) => foo(Some(v / 3))
case Some(v) => Some(v)
case None => None
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment