🎄 7/25. The joy of Unicode in Perl 6

🎄 7/25. The joy of Unicode in Raku

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


Welcome to Day 7 of the Perl 6 One-Liner Advent Calendar! Today, we’ll look at the month of March in the Perl 6 Calendar 2019:

The code here is using three characters outside of the ASCII land. We can even add one more:

say π × $𝜌²

In Perl 6, you can freely use Unicode characters in identifiers, such as variable or function names. But on top of that, there are many pre-defined symbols such as π, which have ASCII alternatives. Examine the documentation page Unicode versus ASCII symbols to see the whole set of Unicode characters that can be used in Perl 6.

Using ASCII, the above one-liner can be re-written in the following way:

say pi * $r ** 2

Let’s return to the bonus code from Day 2 and see where Unicode characters could be used:

sub f($n) {
($n <<*>> (1...1000 / $n)).grep: * < 1000
}

say (f(3) ∪ f(5)).keys.sum;

There are a few opportunities here.

First, a hyper-operator <<*>> can be replaced with proper French quotes: «*», and the multiplication character can be a cross that we used today already: «×». The same can be applied to division: ÷.

Second, the three dots of the sequence operator are replaceable with a single Unicode character:  (if you are programming in Word, you get this character automatically after typing the full stop three times).

Finally, in the last line, a Unicode character is used to find the intersection of two sets. The character here is the same that you use in mathematics (do you?), but you can use its ASCII version instead: f(3) (|) f(5).

So, we’ve got a great Christmas gift, the best Unicode support among programming languages. Use it with care not to make other people crazy about your code! See you tomorrow!

4 thoughts on “🎄 7/25. The joy of Unicode in Perl 6”

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