ðŸ“˜ Adding up two arrays in Perl 6

# ðŸ“˜ Adding up two arrays in Raku

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

Take two arrays and create a new one whose elements are the sums of the corresponding items of the initial arrays.

At first, we assume that the arrays are of the same length. In this case, the easiest way to solve the task is to use a meta-operator:

`my @a = 10..20;my @b = 30..40;my @sum = @a <<+>> @b;say @sum; # [40 42 44 46 48 50 52 54 56 58 60]`

Meta-operators may take a few different forms. In the case of the +operator, it is possible to construct the following meta-operators and their alternatives using the Unicode characters:

`<<+>>     Â«+Â»>>+<<     Â»+Â«>>+>>     Â»+Â»<<+<<     Â«+Â«`

For the above arrays of the same length, all the operators work the same. The difference between them becomes important when the operands are arrays with different numbers of elements. The sharp angles of the arrows must be directed toward the shorter operand.

You can memorise that rule by comparing the operator with the â€˜moreâ€™ and â€˜lessâ€™ operatorsÂ `>` andÂ `<`. The longer array stands at the â€˜biggerâ€™ end. Depending on the operator, the shorter array is either repeated as many times as needed to make pairs of each element of the longer array or is only connected with the first few elements of it. Otherwise, an exception occurs.

Consider the following four combinations with arrays of different lengths:

`my @long = 1, 2, 3, 4;my @short = 10, 20;@long <<+<< @short;@long >>+>> @short;@long <<+>> @short;@long >>+<< @short;`

The first three additions compile and run:

`say @long <<+<< @short; # [11 22]say @long >>+>> @short; # [11 22 13 24]say @long <<+>> @short; # [11 22 13 24]`

As you see, theÂ `<<+<<` operator only addresses the first two elements of the four-element array.

TheÂ `>>+>>` operator repeats the short array twice, and the four valuesÂ `[10, 20, 10, 20]` add up to the four values of theÂ `@long` array.

The most tolerant operator isÂ `<<+>>`. It lets the operands have different lengths and makes the shorter one longer if necessary.

TheÂ `>>+<<` operator requires both operands to have the same length. So, in our example, a runtime error is generated.Â This operator is calledÂ non-dwimmyÂ in the error message because it does not try to adjust to your needs.

`Lists on either side of non-dwimmy hyperop of infix:<+> are not of the same lengthleft: 4 elements, right: 2 elements`

You have to decide whether or not you are going to use the Unicode forms of the operators.