Raku One-Liners — a free book

Let me announce the new book, Raku One-Liners. Electronic editionAmazon Kindle Paperback editionsAmazon.com, Amazon.de (and also on other local Amazon sites) The book is available in the PDF format for free. Paperback copies are available on Amazon. Download the Raku One-Liners book now N.B. As of today, the book is in the test mode, and … Continue reading “Raku One-Liners — a free book”

🦋 110. is rw vs is raw in Raku

The cryptic title should not stop you from seeing bits of the regular Raku code. Namely, the two traits that you can add to function arguments: is rw and is raw. These two traits may look confusing because both allow changing the passed variable: sub f1($x is rw) {     say $x; } sub f2($x is raw) {     say $x; } my $a = 42; f1($a); # 42 f2($a); … Continue reading “🦋 110. is rw vs is raw in Raku”

Using Raku — a free book

Let me announce the second edition of my Using Perl 6 book. This time, it is published under the new name, Using Raku. Electronic editionLeanPubAmazon Kindle Paperback editionsAmazon.com, Amazon.de (and also on other local Amazon sites) The book is available in the PDF format for free. Paperback copies are available on Amazon. Download the Using Raku book … Continue reading “Using Raku — a free book”

📘 The Brainfuck interpreter written in Perl 6

Create the interpreter for the Brainfuck language. Brainfuck is an esoteric programming language that has a small set of instructions, each of them a single punctuation character. It is assumed that the Brainfuck program has built-in data memory, which is an array of integers, and a pointer to the currently selected item. The two instructions, + … Continue reading “📘 The Brainfuck interpreter written in Perl 6”

📘 Converting Morse to text using Perl 6

Convert the Morse sequence to plain text. To save efforts in typing the decoding table, we can use the %code hash from Task 98, Text to Morse code, and create the ‘inversed’ hash, where the keys are the Morse sequences, and the values are letters or digits: my %char = %code.kv.reverse; Printing this variable shows its contents … Continue reading “📘 Converting Morse to text using Perl 6”

📘 Converting text to Morse code using Perl 6

Convert the given text to the Morse code. Converting text to the Morse code is a relatively easy task. The solution is to replace all the alphanumeric characters with the corresponding representation in the Morse code. In this solution, all the other characters are ignored and are removed from the source string. In the Morse … Continue reading “📘 Converting text to Morse code using Perl 6”

📘 Reading directory content using Perl 6

Print the file names from the current directory. Reading a directory in Perl 6 can be done using the dir routine defined in the IO::Path class. say dir(); This tiny program does not do the task really satisfactory, as the dir routine returns a lazy sequence (an object of the Seq data type) of IO::Path objects. To get the textual … Continue reading “📘 Reading directory content using Perl 6”

📘 The uniq utility written in Perl 6

Create the simple equivalent of the UNIX uniqutility, which only prints the lines from the STDIN input, which are not repeating the previous line. The solution of this task can be built on the solution of Task 95, The catutility. This time, the entered lines have to be saved, so let’s introduce the $previous variable and make an explicit … Continue reading “📘 The uniq utility written in Perl 6”

📘 The cat utility written in Perl 6

Create the equivalent of the UNIX catutility that copies its STDIN input directly to STDOUT. Reading from the input and sending it to the output is a relatively easy task. The $*IN handle is by default connected to STDIN. Being an object of the IO::Handle type, it has the slurp method that returns the whole input text in one … Continue reading “📘 The cat utility written in Perl 6”

📘 Parallel file processing in Perl 6

Process the files from the current directory in a few parallel threads. We have to do something with each file in the directory, and it has to be done in such a way that files are processed independently with a few workers. It is not possible to predict how long the process will take for … Continue reading “📘 Parallel file processing in Perl 6”

📘 Atomic operations in Perl 6

Using the solution for Task 38, the Monte Carlo method, create a program that calculates the result using multiple threads. Perl 6 has built-in support for parallel computing. In Task 92, Sleep Sort, we’ve seen how to use the keywords await, gather, and take to spawn a few threads and wait for them to finish.  When different threads want to … Continue reading “📘 Atomic operations in Perl 6”

📘 Sleep Sort in Perl 6

Implement the Sleep Sort algorithm for a few small positive integer values. The Sleep Sortis a funny implementation of the sorting algorithm. For each input value, an asynchronous thread starts, which waits for the number of seconds equals to the input number and then prints it. So, if all the threads are spawned simultaneously, the output … Continue reading “📘 Sleep Sort in Perl 6”

📘 Setting timeouts in Perl 6

Do not wait for a slow code block if it takes too long. In Perl 6, promises are the best way to create timeouts. In the following example, two code blocks are created; they are executed in parallel. my $timeout = Promise.in(2).then({    say ‘Timeout after 2 seconds’;});my $code = start {    sleep 5;    say ‘Done after 5 seconds’;} … Continue reading “📘 Setting timeouts in Perl 6”

📘 Computing leap years in Perl 6

Tell if the given year is leap or common. The algorithm for detecting whether the year is leap includes a few divisibility tests. Take an extract in the pseudocode from Wikipedia: if (year is not divisible by 4) then (it is a common year)else if (year is not divisible by 100) then (it is a leap year)else if (year is not divisible by 400) then (it is a common … Continue reading “📘 Computing leap years in Perl 6”

📘 Datetime arithmetic in Perl 6

Find the difference between the two dates. Add a given number of days to the date. The DateTime class in Perl 6 defines the + and – operators, which can be used in combination with either another DateTime object or with the Duration object. Let us first find the difference between the two given dates: my $date1 = DateTime.new(‘2017-12-31T23:59:50’);my $date2 … Continue reading “📘 Datetime arithmetic in Perl 6”

📘 Formatted date in Perl 6

Print the current date in an even better format. In Perl 6, there is a built-in DateTime class. It is equipped with a few useful methods, so there’s no need to use external modules for many standard tasks. Save the current moment in the $now variable: my $now = DateTime.now; The easiest thing is to print the … Continue reading “📘 Formatted date in Perl 6”

📘 Current date and time in Perl 6

Print current date and time as an epoch and in a human-readable format. In Perl 6, the time function returns the current time as the Unix epoch: say time; The output is something like this: 1495785518. For manipulating dates and times, use the built-in DateTime class: say DateTime.now; The date is now in a more human-readable format, although … Continue reading “📘 Current date and time in Perl 6”

📘 Basic calculator written in Perl 6

Create a program that calculates mathematical operations with two operands, for example: 4 + 5.3 or 7.8 / 3. In this task, we will only limit the solution for the simplest case with only one operation so that there are no issues with the precedence order or parentheses. Let’s first make a solution with regexes … Continue reading “📘 Basic calculator written in Perl 6”