对哈希 kv 对进行排序

标签 sorting hashmap raku

my %hash =
    two   => 2,
    three => 3,
    one   => 1,
;

for %hash.sort(*.key)>>.kv -> ($key, $value) {
    say "'$key' => '$value'";
}

%hash.sort({.key})>>.kv相当于上面的排序?

为什么这种排序在没有超 >> 的情况下不起作用暗示?

最佳答案

sort 方法返回 ListPairs .

由于调用 .kv 在列表上返回 index, Pair 的列表列表,你不想要的;你不能只打电话 .kv .所以你必须从 Pair 中取出键和值。通过调用 .kv 单独列出列表中的对象他们每个人的方法,>>.kv做。

您也可以使用 .map(*.kv) 反而。
>>.kv如果有意义的话,语法允许实现将工作分散到多个线程上。
(目前 Rakudo 只是以半随机顺序完成工作,以防止人们错误地使用该功能)

通过使用子签名中的副词提取属性,还有另一种编写循环的方法:

for %hash.sort -> (:$key, :$value) {
  say "'$key' => '$value'";
}

for %hash.sort -> $pair (:$key, :$value) {
  say $pair;
  say $key === $pair.key and $value === $pair.value; # True␤
}

# :$key is short for :key($key)
for %hash.sort -> (:key($k), :value($v)) {
  say "'$k' => '$v'";
}

这对于没有创建其公共(public)属性列表的方法的其他对象很有用

class C { has $.a; has $.b; has $.c; has $!private-value }
my $c = 5;
my $obj = C.new(:a<A>,:b(1),:$c);

given $obj -> ( :$a, :b($b), :$c) ) {
  say "$a $b $c";
}

# ignore $.a by using an unnamed scalar
given $obj -> ( :a($), :$b, :$c ) { ... }

# places any unspecified public attributes in %others
given $obj -> ( :$a, :$b, *%others ) {
  .say for keys %others; # c␤
}

# ignores any unspecified attributes
# useful to allow subclasses to add more attributes
# or to just drop any values you don't care about
given $obj -> ( :$a, :$b, *% ) { ... }

# fails because it doesn't handle the public c attribute
# in the sub-signature
given $obj -> ( :$a, :$b ) { ... }

这只是签名可能的开始。

以下所有内容在子例程和方法签名中也是允许的,可选的,对于这个例子来说完全是多余的。
它在用于限制可能的候选者的多子和多方法中非常有用。

for 'one' => 1, 1/3
->
  # Type is an alias to the object type
  ::Type Any $_ # Any is the default type requirement

  # the public attributes of the object
  (
    ::A-Type Any :key(   :numerator(   $a ) ),
    ::B-Type Any :value( :denominator( $b ) ) where $b >= 1,
  )
{
  my Type $obj = $_; # new variable declared as having the same type
  my A-Type $new-a = $a;
  my B-Type $new-b = $b;

  # could have used $_.^name or .^name instead of Type.^name
  # so you don't actually have to add the alias to the signature
  # to get the name of the arguments type
  say Type.^name, ' ', $_;
  say '  ', A-Type.^name, ' ', $a;
  say '  ', B-Type.^name, ' ', $b;
}

Pair one => 1
  Str one
  Int 1
Rat 0.333333
  Int 1
  Int 3

至于使用 .sort({.key}) , 是的,这与 sort 基本相同。接受任何东西Callable那里。

我想指出,您甚至不需要向 sort 提供参数。因为它的默认值甚至比你给它的更聪明。

Perl 6 有许多创建和访问 Callable 的方法事物。因此,以下任何一项都会起作用:

*.key
{ .key } # { $_.key }
-> $_ { .key } # basically what the previous line turns into
{ $^placeholder-var.key }
sub ($_) { .key }
&a-subroutine-reference # you would have to create the subroutine though

此外,由于所有普通运算符实际上都是子程序,因此您可以在需要 Callable 的其他地方使用它们。 . (虽然我想不出一个在那个地方工作的人)

&infix:<+> # the subroutines responsible for the numeric addition operator
&[+] # ditto

&prefix:<++>
&postfix:<++>

# etc

关于对哈希 kv 对进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31745631/

相关文章:

arrays - 快速排序字符串中的字符

Java HashMap.put() 无法在 invokeLater createAndShowGui 方法内工作

java - 如何检查Java中HashMap中的键值是否正确

raku - 在 Perl 6 中,如何将模块的 pod 保留在文件的底部,同时仍然使用声明器 block 来记录方法/子程序?

range - 我应该在 Perl 6 中用序列还是范围来计数?

python - python中快速排序的实现和交换枢轴值

python - 将项目附加到列表并通过递增数字确保名称唯一

java - 根据子列表的大小对列表列表进行排序?

Android:如何使用简单适配器将图像文件从 sd 卡放入 HashMap?

raku - 整数位的 Perl6-ish 表达式