Created
March 22, 2018 21:39
-
-
Save jackmaney/cfb535f161b30b325fdebfbb3170a426 to your computer and use it in GitHub Desktop.
Scala's sexy, sexy implicits...
This file contains 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
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