内容简介:The fact that Raku has no explicit references combined with the possibility to omit parentheses can sometimes mislead a developer. In this blog post, I will give a few examples of where I had some issues with trying to make it work.First, here is a data st
The fact that Raku has no explicit references combined with the possibility to omit parentheses can sometimes mislead a developer. In this blog post, I will give a few examples of where I had some issues with trying to make it work.
First, here is a data structure that we’ll play with:
my %capitals = France => 'Paris', Italy => 'Rome';
(I hope you will be able to visit the mentioned countries and the great cities pretty soon.)
The %capitals
variable is a hash that contains two key—value pairs. You can dump it to the console with a built-in Rakudo’s routine dd
:
dd %capitals; # You will see this output: # <strong>Hash %capitals = {:France("Paris"), :Italy("Rome")}</strong>
1
If you are used to work withPerl, make sure you are not using curly braces when accessing the element.
This is correct:
say %capitals<France>; # Paris say %capitals<Italy>; # Rome
This is wrong:
say %capitals{France}; say %capitals{Italy};
Raku expects you have some Raku code inside curly braces, so if you do not define the functions named France
and Italy
, you’ll get a compile error:
===SORRY!=== Error while compiling /Users/ash/raku-test/return-hash.raku Undeclared names: France used at line 11 Italy used at line 12
So, with this kind of brackets, you need either a variable or a string to make it work as expected:
my $country = 'France'; say %capitals{$country}; # Paris say %capitals{'Italy'}; # Rome
2
There is a chance to mislead yourself if you try to think of a hash name as of a reference and make it a scalar:
my $capitals = France => 'Paris', Italy => 'Rome';
This code compiles with a warning:
Useless use of "Italy => 'Rome'" in sink context
For Raku, this code means you are trying to create two independent pairs and assign them to some scalar variables, but there is only one such variable. Dump it to see what it contains:
dd $capitals; # <strong>Pair $capitals = :France("Paris")</strong>
This time, this is not a hash, but a single pair, and the second country—capital pair is lost (that’s what the warning meant).
It is possible to avoid the warning by supplying the compiler with another scalar container:
my ($capital1, $capital2) = France => 'Paris', Italy => 'Rome';
Probably, this is not what you wanted in the beginning. You created a couple of pairs, each keeping a single city:
dd $capital1; # <strong>Pair $capital1 = :France("Paris")</strong> dd $capital2; # <strong>Pair $capital2 = :Italy("Rome")</strong> say $capital1<France>; # Paris say $capital2<Italy>; # Rome;
If you try to cross-access the keys, you get Nil
, as each of the pair contains exactly one key and exactly one value:
say $capital1<Italy>; # <strong>Nil</strong> say $capital2<France>; # <strong>Nil</strong>
3
OK, now let us use curly braces in our attempts to create a hash. First, store it in the variable that comes with the %
sigil:
my <strong>%capitals = {</strong> France => 'Paris', Italy => 'Rome' <strong>}</strong>;
Then, with $
:
my <strong>$capitals = {</strong> France => 'Paris', Italy => 'Rome' <strong>}</strong>;
The fun fact is that you can notice only a tiny difference if you dump the variables:
dd %capitals; # <strong>Hash %capitals = {:France("Paris"), :Italy("Rome")}</strong> dd $capitals; # <strong>Hash $capitals = ${:France("Paris"), :Italy("Rome")}</strong>
When accessing the elements, there is no difference:
say %capitals<France>; # Paris say %capitals<Italy>; # Rome; say $capitals<France>; # Paris say $capitals<Italy>; # Rome;
In Raku, a scalar variable such as $capitals
is just a container that can still keep a hash object.
A $
-prefixed variable contains a single object, and you can easily see it if you iterate over it
. Compare:
.WHAT.say for <strong>%</strong>capitals; # <strong>(Pair)</strong> # <strong>(Pair)</strong> .WHAT.say for <strong>$</strong>capitals; # <strong>(Hash)</strong>
4
If you are not confused so far, then you are prepared to the next example. Define the following two functions that intend to return the same hash data with the country—capital pairs:
sub get_capitals() { return France => 'Paris', Italy => 'Rome'; } sub get_capitals1() { return <strong>{</strong> France => 'Paris', Italy => 'Rome', <strong>}</strong>; }
Notice the differences in how a return object is created. The difference gets less visible when you start using the functions, especially if they are defined in a separate module, for example. Let’s save the results of these two functions in different variables, a scalar and a hash. Limit ourselves to the first function first:
my %data = get_capitals(); my $data = get_capitals();
When you save it to a hash, you get what you want:
dd %data; # <strong>Hash %data = {:France("Paris"), :Italy("Rome")}</strong> say %data<France>; # Paris say %data<Italy>; # Rome
With the scalar variable on the left side of the assignment, things are less straightforward. Unlike the example in section 2, the second pair is not ignored, and you return a list .
dd $data; # <strong>List $data = $(:France("Paris"), :Italy("Rome"))</strong> dd $data[0]; # :France("Paris") say $data<strong>[0]</strong><France>; # Paris say $data<strong>[1]</strong><Italy>; # Rome # say $data<France>; # Type List does not support associative indexing.
As you can see, you get a list of pairs, and you need to explicitly index it to get the values. This may be a very unexpected surprise in practice.
With the second function, the one that use curly braces to build a hash before returning it, chances to make it wrong vanish:
%data = get_capitals1(); dd %data; # <strong>Hash %data = {:France("Paris"), :Italy("Rome")}</strong> say %data<France>; # Paris say %data<Italy>; # Rome $data = get_capitals1(); dd $data; # <strong>Hash $data = ${:France("Paris"), :Italy("Rome")}</strong> say $data<France>; # Paris say $data<Italy>; # Rome
And that’s all for now. You can find the all the examples in the return-hash.raku file in my GitHub repository.
以上所述就是小编给大家介绍的《Some tips for working with hashes in Raku》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
写给大家看的设计书(第3版)
[美] Robin Williams / 苏金国、刘亮 / 人民邮电出版社 / 2009-1 / 49.00元
这本书出自一位世界级设计师之手。复杂的设计原理在书中凝炼为亲密性、对齐、重复和对比4 个基本原则。作者以其简洁明快的风格,将优秀设计所必须遵循的这4 个基本原则及其背后的原理通俗易懂地展现在读者面前。本书包含大量的示例,让你了解怎样才能按照自己的方式设计出美观且内容丰富的产品。 此书适用于各行各业需要从事设计工作的读者,也适用于有经验的设计人员。一起来看看 《写给大家看的设计书(第3版)》 这本书的介绍吧!