πŸ“˜ How to reverse a list in Perl 6

πŸ“˜ How to reverse a list in Raku

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

Print the given list in reverse order.

Start with an array of integer numbers.

my @a = (10, 20, 30, 40, 50);

To reverse the array, call the reversemethod on it.

say @a.reverse;

This line prints the required result:

(50 40 30 20 10)

Notice that the initial array stays unchanged. The reversemethod creates a new sequence and returns it.

The same method works with other types of data that can be converted to a sequence, for example, ranges.

Print a range in reversed order:

my $range = 10..15;
say $range;
say $range.reverse;

Again, the original range is not changed, and the returned value is not a range but a sequence. Compare the results of printing an original value ofΒ $range with what theΒ reverse method returns:

(15 14 13 12 11 10)

Leave a Reply

Your email address will not be published. Required fields are marked *

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code