๐ŸŽ„ 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 to cxw Cancel 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