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

栏目: IT技术 · 发布时间: 5年前

内容简介: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 :-).Table of ContentsThere is a pair of transliteration (or transformation) operators in Ra

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 :-).

Table of Contents

tr ad 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 ~~ <strong>tr</strong>/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 = <strong>TR</strong>/o/ó/ <strong>with</strong> $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!';

<strong>my $r = $s ~~ tr/o/ó/;</strong>

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 <strong>~</strong>$r; # Helló, Wórld!
say <strong>+</strong>$r; # 2

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

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

put vs get

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:

<strong>put</strong> $r; # Helló, Wórld!

With say , you see more details about the contents:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

程序员的数学思维修炼(趣味解读)

程序员的数学思维修炼(趣味解读)

周颖 / 清华大学出版社 / 2014-4-1 / 45.00元

本书是一本专门为程序员而写的数学书,介绍了程序设计中常用的数学知识。本书门槛不高,不需要读者精通很多高深的数学知识,只需要读者具备基本的四则运算、乘方等数学基础知识和日常生活中的基本逻辑判断能力即可。本书拒绝枯燥乏味的讲解,而是代之以轻松活泼的风格。书中列举了大量读者都很熟悉,而且非常有趣的数学实例,并结合程序设计的思维和算法加以剖析,可以训练读者的数学思维能力和程序设计能力,进而拓宽读者的视野,......一起来看看 《程序员的数学思维修炼(趣味解读)》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具