Course of Raku / Advanced / Operators / Meta-operators / Exercises / Every factorial at once

Solution: Every factorial at once

Here is a possible solution to the task.

Code

say [\*] 1..6;

🦋 You can find the source code in the file factorial.raku.

Output

(1 2 6 24 120 720)

Comments

  1. The backslash inside the brackets turns [*] into a triangular reduction. Instead of collapsing the list to a single value, it keeps every partial product: 1, then 1*2, then 1*2*3, and so on.

  2. Each partial product 1*2*...*k is exactly k!, so the result is the list of factorials of 1 through 6. A plain [*] 1..6 would have given only the last of these, 720.

Course navigation

Every factorial at once   |   Build a URL