Skip to content

Instantly share code, notes, and snippets.

@sergiilagutin
Created March 9, 2015 06:29
Show Gist options
  • Select an option

  • Save sergiilagutin/bf9b13d999c8c73c5009 to your computer and use it in GitHub Desktop.

Select an option

Save sergiilagutin/bf9b13d999c8c73c5009 to your computer and use it in GitHub Desktop.
Scala factorial using TDD
// 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