📘 Passing arrays to subroutines in Perl 6

Pass data, contained in an array, to a subroutine. In Perl 6, an array can be passed to a subroutine as easily as a scalar. You simply define it in a signature and pass it together with other arguments. my @colours = <red green blue>;sub f(@data, $sep) {    @data.join($sep).say;}f(@colours, ‘, ‘); # Prints: red, green, blue The @colours array … Continue reading “📘 Passing arrays to subroutines in Perl 6”

📘 Increasing sequences in Perl 6

Check if the given array contains increasing (or decreasing) numbers. Given the list of numbers in an array, the task is to tell if all of them are sorted in ascending or descending order. Take an array: my @data = 3, 7, 19, 20, 34; In Perl 6, reduction operators offer a very expressive and … Continue reading “📘 Increasing sequences in Perl 6”

📘 Finding minimum and maximum using Perl 6

Find the minimum and the maximum numbers in the given list of integers. Finding the minimum and maximum elements of arrays is extremely easy in Perl 6. For iterable objects, the two methods, min and max, are defined. my @list = 7, 6, 12, 3, 4, 10, 2, 5, 15, 6, 7, 8, 9, 3;say @list.min;say @list.max; For the … Continue reading “📘 Finding minimum and maximum using Perl 6”

📘 Finding unique elements using Perl 6

Print all unique elements of the given array. In Perl 6, objects of the Array type have the unique method. my @a = 2, 3, 7, 4, 5, 5, 6, 2, 10, 7;say @a.unique; The result of running this program is a sequence containing the unique elements: (2 3 7 4 5 6 10). Notice that the values … Continue reading “📘 Finding unique elements using Perl 6”

📘 Counting the number of occurrences in array using Perl 6

Count how many times a particular element appears in the array. Let’s have an array with a list of fruits; some of them are repeated. my @data = <     apple       pear      grape     lemon     peach       apple      banana   grape      pineapple  avocado >; Now, the task is to count how many times the element ‘grape’ appears there. First, extract all the elements to a temporary array … Continue reading “📘 Counting the number of occurrences in array using Perl 6”

📘 Taking every second element using Perl 6

Form a new array by picking every second element from the original array. There is an array of numbers, and the task is to pick every second element and put them into a new array. Prepare the test data: my @data = 20..30; Here is a possible solution: my @selected = @data[1, 3 … *];say … Continue reading “📘 Taking every second element using Perl 6”

📘 Finding the first odd number using Perl 6

Find the first odd number in a list of integers. The task is to find the first odd number in a given list of odd and even numbers. A good candidate is the firstroutine, which searches for the leftmost value (see, for example, Task 58, Is an element in a list?). Now, we can pass an anonymous … Continue reading “📘 Finding the first odd number using Perl 6”

📘 Check if an element is in a list in Perl 6

Tell if the given value is in the list. There are a few approaches to the problem. The most compact one seems to be the use of the smartmatch ~~ operator in combination with the anyfunction: my @array = (10, 14, 0, 15, 17, 20, 30, 35);my $x = 17;say ‘In the list’ if $x ~~ any … Continue reading “📘 Check if an element is in a list in Perl 6”

📘 Computing the moving average using Perl 6

Calculate the moving average for the given array of numbers. For each element of an array, the moving average is the average value of the last few items or the few elements around it. This kind of analysis is often used to smooth the curve. Let us first generate some random data—an array of a … Continue reading “📘 Computing the moving average using Perl 6”

📘 Computing the average value of an array using Perl 6

Find the average value of the given array of numbers. Calculating the average value of an array has two subtasks—calculate the sum and divide it by the size of the array. So, one of the solutions can look like this: my @data = 7, 11, 34, 50, 200;say sum(@data) / @data; Here, the sum built-in function … Continue reading “📘 Computing the average value of an array using Perl 6”

📘 Sum of the elements of an array using Perl 6

Find the sum of the elements of an array of integers. There is an array of integers: my @a = (4, 6, 8, 1, 0, 58, 1, 34, 7, 4, 2); There is no need to explicitly iterate over the elements to calculate the sum of its elements. Rather use the reduction operator: say [+] @a; … Continue reading “📘 Sum of the elements of an array using Perl 6”

📘 Exclusion of two arrays in Perl 6

From the given two arrays, find the elements of the first array which do not appear in the second one. Take two arbitrary arrays of integers: my @a = 2, 5, 7, 8, 10;my @b = 1, 3, 5, 7, 9; The program should print 2, 8, and 10. Here is a possible solution: my … Continue reading “📘 Exclusion of two arrays in Perl 6”

📘 Adding up two arrays in Perl 6

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 … Continue reading “📘 Adding up two arrays in Perl 6”

📘 Incrementing array elements in Perl 6

Increment each element in an array. In Perl 6, there is no need to explicitly iterate over an array to apply some operation to each element. Use a hyper-operator: @data>>++; Let us try it on a small array: my @data = 1..10;@data>>++;say @data; # [2 3 4 5 6 7 8 9 10 11] The >>++ operator is … Continue reading “📘 Incrementing array elements in Perl 6”

📘 How to randomise an array in Perl 6

Shuffle the elements of an array in random order. Arrays in Perl 6 have the pick method, which does the work. my @a = 1..20;say@a.pick(@a); A possible output of the program looks like this: (4 18 10 15 14 8 2 11 3 12 1 6 9 19 13 7 16 17 20 5) The pick method … Continue reading “📘 How to randomise an array in Perl 6”

📘 How to rotate a list in Perl 6

Move all elements of an array N positions to the left or to the right. Array is a data type in Perl 6 that offers the rotate method, which does exactly what is needed. It takes an argument that tells the length and direction of the rotation. my @a = (1, 3, 5, 7, 9, 11, … Continue reading “📘 How to rotate a list in Perl 6”

📘 How to reverse a list in Perl 6

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 … Continue reading “📘 How to reverse a list in Perl 6”

📘 Swap two values in Perl 6

Swap the values of two variables. In Perl 6, there is no need to use temporary variables to swap the values of two variables. Just use the lists on both sides of the equation: ($b, $a) = ($a, $b); Alternatively, call the reverse method and assign the result back to the values: ($a, $b).=reverse; Consider the … Continue reading “📘 Swap two values in Perl 6”

📘 Spelling numbers using Perl 6

Write an integer number below one million in words. Human languages have many inconsistencies, especially in the most frequent constructs. Spelling numbers seems to be a simple task, but due to a number of small differences, the resulting program is quite big. The program is listed on the next page. Let’s discuss the algorithm first. … Continue reading “📘 Spelling numbers using Perl 6”

📘 Convert to Roman numerals using Perl 6

Convert an integer number to a Roman numerals string. Roman numbers are not a direct translation of the decimal system. In this task, we assume that the number is not morethan 3999, which is the maximum a regular Roman number can reach. Let’s use the algorithm that keeps the table of pre-calculated sequences of Roman letters … Continue reading “📘 Convert to Roman numerals using Perl 6”