Last active
December 19, 2015 08:39
-
-
Save MattOates/5927100 to your computer and use it in GitHub Desktop.
A Perl6 factorial postfix operator using recursive multi dispatch
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
multi sub postfix:<!> (Int $i where $i < 0) is tighter(&infix:<*>) { fail "Not a Natural Number in Factorial" } | |
multi sub postfix:<!> (Int $i where 0|1) is tighter(&infix:<*>) { 1 } | |
multi sub postfix:<!> (Int $i where $i > 1) is tighter(&infix:<*>) { $i * ($i-1)! } | |
use Test; | |
isa_ok -1!, Failure, "Factorial for -1 fails"; | |
ok 0! == 1, "Factorial for 0"; | |
ok 1! == 1, "Factorial for 1"; | |
ok 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