是否有类似于Python中的Counter的内置方法?

Sta*_*224 9 perl6

我已经挖掘了文档,但我似乎找不到类似于Python中的Counter.

我知道我可以写一些类似的东西,但内置会很方便.

琐碎的例子: my %h; %h{$_}++ for @test;

Eli*_*sen 12

类你想要做什么.

my %h is Bag = @test;
Run Code Online (Sandbox Code Playgroud)

或者如果你只想强迫:

my $bag = @test.Bag;
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,你可以像使用任何普通物体Hash.

# show sorted with most frequent first
say "{.key} seen {.value} times" for %h.sort: -*.value
Run Code Online (Sandbox Code Playgroud)