๐Ÿ“˜ Reverse a string using Perl 6

Print a string in the reversed order from right to left. Strings, or the objects of the Str class, have the flip method, which does the work: my $string = ‘Hello, World!’;say $string.flip; This code prints the desired result: !dlroW ,olleH Theย flip routine may be called both as a method on a string and as a stand-alone … Continue reading “๐Ÿ“˜ Reverse a string using Perl 6”

๐Ÿ“˜ Finding unique digits using Perl 6

Print unique digits from a given integer number. The task is easily solved if an integer is immediately converted to a string. my $number = prompt(‘Enter number> ‘);say $number.comb.unique.sort.join(‘, ‘); The combmethod, called with no arguments, splits the string into separate characters. The method is defined in the Str class; thus, the $number is converted to the string … Continue reading “๐Ÿ“˜ Finding unique digits using Perl 6”

๐Ÿ“˜ String length in Perl 6

Print the length of a string. Perl 6 handles all strings as UTF-8 by default. This is why there is more than one parameter describing the length of the string. In fact, the lengthroutine does not exist, and an attempt to use it issues an error message with some hints to which other methods you can … Continue reading “๐Ÿ“˜ String length in Perl 6”

๐Ÿ“˜ Greet a person using Perl 6

Ask a user for their name and greet them by printing โ€˜Hello, <Name>!โ€™ Perl 6 offers a simple promptfunction that performs both actions: prints a prompt and reads the input. So, the program using it may look like this: say ‘Hello, ‘ ~ prompt(‘Enter your name: ‘) ~ ‘!’; The ~operator stands for string concatenation in Perl 6. Donโ€™t … Continue reading “๐Ÿ“˜ Greet a person using Perl 6”

๐Ÿ“˜ Hello, World! in Perl 6

Print โ€˜Hello, World!โ€™ There are two built-in functions in Perl 6 to print to the console: printand say. Both print their arguments, but the say routine additionally ends the output with a newline character. So, the quickest solution is to use sayand pass a string with no newlines: say ‘Hello, World!’ Another solution is to use print and include the \n character … Continue reading “๐Ÿ“˜ Hello, World! in Perl 6”

๐Ÿ“˜ How to debug Perl 6 programs

For quick tests, use the compiler in the mode of the REPL (readโ€”evalโ€”print loop) shell. Just run theย perl6 command: $ perl6To exit type ‘exit’ or ‘^D’>  With bigger programs, one of the following techniques helps to visualise data: 1. Theย say routine is used as a stand-alone function or as an object method. It works well … Continue reading “๐Ÿ“˜ How to debug Perl 6 programs”

๐Ÿฆ‹ 109. 42 via the cubes

In the recent days, you might have seen the calculation that leads to getting an exact value of 42, the answer of Life, the Universe and Everything. Let me copy it here, using the power of Perl 6 and its arbitrary precision arithmetics , not to mention the coolness of using superscripts directly in the … Continue reading “๐Ÿฆ‹ 109. 42 via the cubes”

๐Ÿฆ‹ 108. Basic usage of NativeCall in Perl 6

NativeCall is both a module and a technology in Perl 6 that allows you to call C functions from your Perl 6 code. Today, letโ€™s meet the most basic usage. Take the rand() function from the C standard library: #include <stdio.h> #include <stdlib.h> int main() { int r = rand(); printf(“%i\n”, r); return 0; } … Continue reading “๐Ÿฆ‹ 108. Basic usage of NativeCall in Perl 6”

๐Ÿ’ก 107. Odd-even sort in Perl 6

In the Odd-Even sort, or Brick sort, you take every second element and swap it with the next one if they are not sorted. Then you take the same elements and swap it with the previous element if they are not ordered. You continue until you are done. You can formulate the algorithm a bit … Continue reading “๐Ÿ’ก 107. Odd-even sort in Perl 6”

๐Ÿ’ก 106. Gnome sort in Perl 6

Our todayโ€™s topic is the Gnome sort, which is also referred to as Stupid sort. To sort an array using this method, you scan the data from left to right and check the two adjacent items to see if they are ordered properly. If they are, you go forward. If not, you swap the elements … Continue reading “๐Ÿ’ก 106. Gnome sort in Perl 6”

๐Ÿ’ก 105. Pancake sort in Perl 6

The Pancake sort is an interesting method of sorting data, as unlike more traditional sorting algorithms, it operates with piles of data on each step. You have to imagine data as a pile of pancakes, the values corresponding to the size of pancakes. The only allowed operation is flipping a pile of โ€˜pancakes.โ€™ It can … Continue reading “๐Ÿ’ก 105. Pancake sort in Perl 6”

๐Ÿ’ก 104. Stooge sort in Perl 6

Have you ever heard of the Stooge sort algorithm? If not, it is quite interesting to learn it. The idea is clear, while you maybe need some time to see if it really works. So, take a list of numbers and swap the first and the last elements if they are not sorted properly (that … Continue reading “๐Ÿ’ก 104. Stooge sort in Perl 6”

๐Ÿ’ก 103. Merge sort in Perl 6

Welcome to another sorting episode, this time weโ€™ll talk about Merge sort in Perl 6. In Merge sort, you first split the data into halves until the pieces become atomic (in the original meaning of the word), that is either each piece contains a single element, or, after the current split, the second part contains … Continue reading “๐Ÿ’ก 103. Merge sort in Perl 6”

๐Ÿ’ก 102. Insertion sort in Perl 6

Today, we are investigating the Insertion sort algorithm and its possible implementation in Perl 6. The algorithmโ€™s complexity is O(n2), but it is a good candidate to practice some Perl 6. The idea is simple. You find the minimum value in the array and put it to the first position. Then you scan the data … Continue reading “๐Ÿ’ก 102. Insertion sort in Perl 6”

๐Ÿ’ก 101. Quick sort in Perl 6

Today, letโ€™s look at another, and presumably the most well known method of sorting data, Quick sort. The algorithm requires you to select the so-called pivot, one of the element from the data, and split the rest in two parts: the elements less than the pivot, and the elements more or equals to the pivot. … Continue reading “๐Ÿ’ก 101. Quick sort in Perl 6”

๐Ÿ’ก 100. Bubble sort in Perl 6

Hey everyone, letโ€™s implement some algorithms in Perl 6. The first one will be the classical Bubble sort. In essence, you scan the data from left to right and swap the two adjacent items if they are not ordered properly yet. Repeat until the whole data list is ordered. Hereโ€™s the initial straightforward implementation: sub … Continue reading “๐Ÿ’ก 100. Bubble sort in Perl 6”

๐Ÿ“บ Creating a compiler in Perl 6

At the German Perl Workshop 2019 in Munich, I gave a presentation about how to create compilers and interpreters using Perl 6 grammars. This talk differs from my previous talks on this subject, so if youโ€™ve seen them, I hope you will also enjoy this one. There was a video recording during the talk, I … Continue reading “๐Ÿ“บ Creating a compiler in Perl 6”

๐Ÿ“บ Perl 6 as a new tool for language compilers

Here’s my recent talk from FOSDEM in Brussels, given on 3 February 2019. Perl 6 grammars are a great way to describe the grammar and implement an interpreter or a compiler of DSL or a programming language. In this talk, I will demonstrate how you can do it. During the talk, we will create an … Continue reading “๐Ÿ“บ Perl 6 as a new tool for language compilers”

๐ŸŽ„ 26/25. Overview of the Perl 6 One-Liner Advent Calendar 2018

The Perl 6 One-Liner Advent Calendar 2018 is over! Letโ€™s make a quick overview of what we have covered so far. There were a few themes covered. First, some one-liners from the Perl 6 Calendar 2019 were explained in more detail. We looked at how to generate random passwords and random integers, how to print … Continue reading “๐ŸŽ„ 26/25. Overview of the Perl 6 One-Liner Advent Calendar 2018”