Matt's Programming Blag

Snippets of code to save you time.

Perl6

A familiar example of a factorial function using a switch (given/when) statement:

Using the meta [*] list operator we can calculate the product of a range 1 upto the desired number:

Finally we can introduce a postfix ! operator for producing the factorial of a number, 5! == 120 being true.