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”
Category: Raku
๐ 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”
๐บ Perl 6 One-Liners slides
Here are the slides of my talk at the German Perl Workshop 2019 in Munich, which summarises a Christmas series of posts written last year. Lots of Perl 6 one-liners and related stuff such as the usage of the MAIN function. Video recording should also appear soon. Update. The video is ready.
๐บ 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”