Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
BagHash/MixHash accept values of correct type only
  • Loading branch information
lizmat committed Oct 4, 2015
1 parent b14723d commit 2765b6a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/core/BagHash.pm
Expand Up @@ -20,7 +20,8 @@ my class BagHash does Baggy {
},
STORE => -> $, $value is copy {
if $value > 0 {
(%!elems.AT-KEY(k.WHICH) //= ((k) => my $ = 0)).value = $value;
(%!elems.AT-KEY(k.WHICH) //=
((k) => my Int $ = 0)).value = $value;
}
elsif $value == 0 {
%!elems.DELETE-KEY(k.WHICH);
Expand Down
6 changes: 3 additions & 3 deletions src/core/Baggy.pm
Expand Up @@ -145,17 +145,17 @@ my role Baggy does QuantHash {
multi method new(Baggy: +@args) {
my %e;
# need explicit signature because of #119609
-> $_ { (%e{$_.WHICH} //= ($_ => my $ = 0)).value++ } for @args;
-> $_ { (%e{$_.WHICH} //= ($_ => my Int $ = 0)).value++ } for @args;
self.bless(:elems(%e));
}
method new-from-pairs(*@pairs) {
my %e;
for @pairs {
when Pair {
(%e.AT-KEY($_.key.WHICH) //= ($_.key => my $ = 0)).value += $_.value.Int;
(%e.AT-KEY($_.key.WHICH) //= ($_.key => my Int $ = 0)).value += $_.value.Int;
}
default {
(%e.AT-KEY($_.WHICH) //= ($_ => my $ = 0)).value++;
(%e.AT-KEY($_.WHICH) //= ($_ => my Int $ = 0)).value++;
}
}
my @toolow;
Expand Down
3 changes: 2 additions & 1 deletion src/core/MixHash.pm
Expand Up @@ -20,7 +20,8 @@ my class MixHash does Mixy {
},
STORE => -> $, $value is copy {
if $value != 0 {
(%!elems.AT-KEY(k.WHICH) //= ((k) => my $ = 0)).value = $value;
(%!elems.AT-KEY(k.WHICH) //=
((k) => my Real $ = 0)).value = $value;
}
else {
%!elems.DELETE-KEY(k.WHICH);
Expand Down
12 changes: 10 additions & 2 deletions src/core/Mixy.pm
Expand Up @@ -2,14 +2,22 @@ my role Mixy does Baggy {
method default(--> Real) { 0 }
method total(--> Real) { [+] self.values }

multi method new(Mixy: +@args) {
my %e;
# need explicit signature because of #119609
-> $_ { (%e{$_.WHICH} //= ($_ => my Real $ = 0)).value++ } for @args;
self.bless(:elems(%e));
}
method new-from-pairs(*@pairs --> Mixy) {
my %e;
for @pairs {
when Pair {
(%e.AT-KEY($_.key.WHICH) //= ($_.key => my $ = 0)).value += $_.value;
(%e.AT-KEY($_.key.WHICH) //=
($_.key => my Real $ = 0)).value += $_.value;
}
default {
(%e.AT-KEY($_.WHICH) //= ($_ => my $ = 0)).value++;
(%e.AT-KEY($_.WHICH) //=
($_ => my Real $ = 0)).value++;
}
}
for %e -> $p {
Expand Down

0 comments on commit 2765b6a

Please sign in to comment.