Last active
December 19, 2015 08:39
-
-
Save MattOates/5927320 to your computer and use it in GitHub Desktop.
A factorial function in Perl6 using a [*] meta operator to produce the factorial product of a range, additionally the function parameter is given using a placeholder variable $^i
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
sub factorial { | |
if $^i < 0 { | |
fail "Not a Natural Number"; | |
} else { | |
[*] 1..$i; | |
} | |
} | |
use Test; | |
isa_ok factorial(-1), Failure, "Factorial for -1 fails"; | |
ok factorial(0) == 1, "Factorial for 0"; | |
ok factorial(1) == 1, "Factorial for 1"; | |
ok factorial(5) == 120, "Factorial for a larger integer"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment