A couple of syntax sweets in Raku

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

内容简介:Whenworking on preparing data for theThe first construct allows us to scan two different arrays in a single loop. For example, let’s print a table with translations of weekdays:A traditional way would be to use an index:

Whenworking on preparing data for the covid.observer site, I discovered a couple of interesting findings, which I did not notice earlier or did not pay much attention to it.

Scanning two arrays together

The first construct allows us to scan two different arrays in a single loop. For example, let’s print a table with translations of weekdays:

my @English = <Monday Tuesday Wednesday Thursday
               Friday Saturday Sunday>;
my @Latvian = <pirmdiena otrdiena trešdiena ceturtdiena 
               piektdiena sestdiena svētdiena>;

A traditional way would be to use an index:

for 0 ..^ @English -> $index {
    say "@English[$index] = @Latvian[$index]";
}

In Raku, you can use the Z meta-operator to create a sequence of lists so that you get one element from each array (actually, lists are expected as input) at a time:

for @English <strong>Z</strong> @Latvian -> <strong>($english, $latvian)</strong> {
    say "$english is $latvian in Latvian.";
}

Beware to use parentheses as otherwise you get two two-element lists. Here is the modification that extracts the elements from the list inside the loop body:

for @English Z @Latvian -> <strong>$list</strong> {
    # say $list.^name; # List
    say "<strong>$list[0]</strong> (en)\t= <strong>$list[1]</strong> (lv)";
}

Using [-]

Another interesting use case is the usage of the reduction operator. For me, the most often usage would be getting a sum via [+] :

my @data = 1, 2, 3, 4, 5;
say <strong>[+]</strong> @data; # 15

And I used this for the above-mentioned site to get some totals, for example:

[+] %totals.values.map: *<confirmed>;
%daily-total{$date} = [+] %per-country.values;

Although I know that having the [+] form means putting + between the elements, I did not actually think that the sign before the first element is also a plus, but it is not the + from the surrounding meta-operator.

I had to get the number of active cases of Coronavirus, and thus the formula was straightforward:

my %data =
    confirmed => 100,
    failed    => 1,
    recovered => 70;

my $active = %data<confirmed> - %data<failed> - %data<recovered>;
say $active;

This can be re-written in a compact form with [-] applied to the slice:

my $active = <strong>[-] %data<confirmed recovered failed></strong>;
say $active;

The first element, %data<confirmed> , enters as is (with a unary + in mind), while the other two items are subtracted. Which is exactly what I needed.

I hope you like my two simple but very useful findings.

The source codes with the tests from this post are available on GitHub. The source code of an ad-hoc script generating static pages for the website can be found in its own repository .


以上所述就是小编给大家介绍的《A couple of syntax sweets in Raku》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

技术的本质

技术的本质

布莱恩•阿瑟(Brian Arthur) / 曹东溟、王健 / 浙江人民出版社 / 2014-4-1 / 62.90

★《技术的本质》是复杂性科学奠基人、首屈一指的技术思想家、“熊彼特奖”得主布莱恩•阿瑟所创建的一套关于技术产生和进化的系统性理论,本书是打开“技术黑箱”的钥匙,它用平实的语言将技术最本质的思想娓娓道来。 ★技术,是一个异常美丽的主题,它不动声色地创造了我们的财富,成就了经济的繁荣,改变了我们存在的方式。尽管技术如此重要,却少有人在快节奏的生活中停下来深入思考技术。我们了解技术的原理,却不知道......一起来看看 《技术的本质》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具