Skip to content

Instantly share code, notes, and snippets.

@jackmaney
Created March 22, 2018 21:39
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 jackmaney/cfb535f161b30b325fdebfbb3170a426 to your computer and use it in GitHub Desktop.
Save jackmaney/cfb535f161b30b325fdebfbb3170a426 to your computer and use it in GitHub Desktop.
Scala's sexy, sexy implicits...
implicit class Factorial(val n: Int) extends AnyVal{
def `!`: BigInt = n match {
case _ if n < 0 => throw new IllegalArgumentException()
case 0 => 1L
case _ => (1 to n).foldLeft(1L)(_*_)
}
}
defined class Factorial
@ 5!
res3: BigInt = 120
@ 0!
res4: BigInt = 1
@ 2!
res5: BigInt = 2
@ 3!
res6: BigInt = 6
@ for(i <- 1 to 20){println(i!)}
1
2
6
24
120
720
5040
40320
362880
3628800
39916800
479001600
6227020800
87178291200
1307674368000
20922789888000
355687428096000
6402373705728000
121645100408832000
2432902008176640000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment