/
QuantHash.pm
55 lines (51 loc) · 1.36 KB
/
QuantHash.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
my role QuantHash does Associative {
method Int ( --> Int) { self.total.Int }
method Num ( --> Num) { self.total.Num }
method Numeric ( --> Numeric) { self.total.Numeric }
method Real ( --> Real) { self.total.Real }
method minpairs ( --> List ) {
my @found;
my $min = Inf;
for self.pairs {
my $value := .value;
if $value > $min {
next;
}
elsif $value < $min {
@found = $_;
$min = $value;
}
else {
@found.push: $_;
}
}
@found;
}
method maxpairs ( --> List ) {
my @found;
my $max = -Inf;
for self.pairs {
my $value := .value;
if $value < $max {
next;
}
elsif $value > $max {
@found = $_;
$max = $value;
}
else {
@found.push: $_;
}
}
@found;
}
method fmt(QuantHash: Cool $format = "%s\t\%s", $sep = "\n") {
if nqp::p6box_i(nqp::sprintfdirectives( nqp::unbox_s($format.Stringy) )) == 1 {
self.keys.fmt($format, $sep);
}
else {
self.pairs.fmt($format, $sep);
}
}
}
# vim: ft=perl6 expandtab sw=4