Skip to content

Instantly share code, notes, and snippets.

@MattOates
Last active December 19, 2015 08:39
Show Gist options
  • Save MattOates/5927100 to your computer and use it in GitHub Desktop.
Save MattOates/5927100 to your computer and use it in GitHub Desktop.
A Perl6 factorial postfix operator using recursive multi dispatch
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