Created
June 23, 2013 19:43
-
-
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.
This file contains hidden or 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 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 | |
} |
This file contains hidden or 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 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