Created
March 9, 2015 06:29
-
-
Save sergiilagutin/bf9b13d999c8c73c5009 to your computer and use it in GitHub Desktop.
Scala factorial using TDD
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
| // step 1 | |
| object Factorial { | |
| def fact(n: Int) = | |
| if (n == 2) 2 | |
| else if (n == 5) 120 | |
| else 1 | |
| } | |
| // step 2 | |
| object Factorial { | |
| def fact(n: Int) = | |
| if (n == 2) 1*2 | |
| else if (n == 5) 1*2*3*4*5 | |
| else 1 | |
| } | |
| // step 3 | |
| object Factorial { | |
| def fact(n: Int) = | |
| if (n == 2) (1 to 2).product | |
| else if (n == 5) (1 to 5).product | |
| else (1 to 1.max(n)).product | |
| } | |
| // step 4 | |
| object Factorial { | |
| def fact(n: Int) = | |
| if (n == 2) (1 to 1.max(n)).product | |
| else if (n == 5) (1 to 1.max(n)).product | |
| else (1 to 1.max(n)).product | |
| } | |
| // step 5 | |
| object Factorial { | |
| def fact(n: Int) = | |
| (1 to 1.max(n)).product | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment