ðŸ“˜ Building the product table using Perl 6

# ðŸ“˜ Building the product table using Raku

N. B. Perl 6 has been renamed to Raku. Click to read more.

Generate and print the product table for the values from 1 to 10.

The task does not say anything about how to format the output.

First, let us print the results as a list with one line per one multiplication. In Perl 6, there is a cross operatorÂ `X`, which operates over lists and creates a cross product of them. Each element of the result list is a list of two elements coming from each of the operands of theÂ `X` operator.

`say "\$_[0]Ã—\$_[1] = {[*] @\$_}" for 1..10 X 1..10;`

In each iteration, the loop variableÂ `\$_` receives a list of two elements. They are printed inside the interpolated list:Â `\$_[0]Ã—\$_[1]`. The string in double quotesÂ alsoÂ contains a block of code in curly braces, which is executed as a regular Perl 6 code.Â

The reduction operation is used here to multiply the two elements. Of course, it is possible to do multiplication directly:Â `\$_[0]*\$_[1]`.

The output looks like this:

`1Ã—1 = 11Ã—2 = 21Ã—3 = 3. . .10Ã—8 = 8010Ã—9 = 9010Ã—10 = 100`

Now, let us print the result in the form of a table and try minimizing the code starting with two loops:

`for 1..10 -> \$x {    for 1..10 -> \$y {        print \$x * \$y ~ "\t";    }    print "\n";}`

As the loop body of the inner cycle contains only one statement, it is possible to rewrite it by using the postfix forloop:

`for 1..10 -> \$x {    print "{\$x * \$_}\t" for 1..10;    print "\n";}`

Finally, join the output using theÂ `join` function, which also helps to eliminate trailing tabulation characters at the end of lines:

`for 1..10 -> \$x {    say join("\t", map {\$x * \$_}, 1..10);}`

It is also possible to call the functions as methods on lists:

`for 1..10 -> \$x {   (1..10).map({\$x * \$_}).join("\t").say;}`

Further optimization isnâ€™t easy because two variables are needed for multiplication, while only oneÂ `\$_` can be used as a default loop variable. Now the result is a proper table:

```1Â Â 2Â 3Â 4Â Â 5Â Â 6Â Â 7Â Â 8Â Â 9Â Â 10
2Â Â 4Â 6Â 8Â Â 10 12Â 14 16 18 20
3Â Â 6Â 9Â 12 15 18 21 24 27 30
. . .```