The Pearls of Raku, Issue 3: tr, TR, and StrDistance

How do tr, TR string replacements work in Raku. What is a StrDistance type? What happens when you print such objects with ‘put’ and ‘say’?

Hello! Today, let me show three small but interesting things that you might skip when reading the documentation to the Raku programming language (if you did read it :-).

tr and TR

There is a pair of transliteration (or transformation) operators in Raku: tr and TR. As you may guess from the name, they have something in common. But there are also some differences.

The lower case tr performs an in-place substitution:

my $s = 'Hello, World!';

$s ~~ tr/o/ó/;

say $s; # Helló, Wórld!

The target string is updated after the operation.

Unlike that, TR does not change the string and returns the new string as a result.

my $s = 'Hello, World!';

my $r = TR/o/ó/ with $s;

say $s; # Hello, World!
say $r; # Helló, Wórld!

Note that you cannot simply write my $r = $s ~~ TR/o/ó/, but instead you need to set the topic first, for example, using with. You can do the same with the lowercase version, too:

tr/o/ó/, .say with $s; # Helló, Wórld!

StrDistance

OK, what happens if you save the result of matching with tr as in the following snippet?

my $s = 'Hello, World!';

my $r = $s ~~ tr/o/ó/;

Unlike the probable expectation, you do not simply get the new string in $r. The type of that object is StrDistance.

say $r.WHAT; # (StrDistance)

When being coerced to a string, it gives you the string—the modified string after the replacement in our case. When it is coerced to an integer, you get the number of replacements happened:

say ~$r; # Helló, Wórld!
say +$r; # 2

The variable also keeps both original and modified versions of the string:

say $r.before;
say $r.after;

put vs say

The third thing to cover in this post is to see the difference between put and say, when they are called with the StrDistance object.

The difference between put and say is that put internally calls .Str on an object, while say calls .gist.

From the previous section, we know that if a StrDistance is converted to a string, it gives a string, so in our example, you see the updated string after the transliteration:

put $r; # Helló, Wórld!

With say, you see more details about the contents:

say $r; 
# StrDistance.new(before => "Hello, World!", after => "Helló, Wórld!")

Leave a Reply

Your email address will not be published. Required fields are marked *