Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Change .elems to .total for Sets/Bags, .elems is now what you expect …
…for a hash
  • Loading branch information
lizmat committed Oct 3, 2013
1 parent eb07bd1 commit 0dbbbfe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/core/Baggy.pm
Expand Up @@ -5,7 +5,8 @@ my role Baggy does QuantHash {
method default(--> Int) { 0 }
method keys { %!elems.values.map( {.key} ) }
method values { %!elems.values.map( {.value} ) }
method elems(--> Int) { [+] self.values }
method elems(--> Int) { %!elems.elems }
method total(--> Int) { [+] self.values }
method exists ($k --> Bool) { # is DEPRECATED doesn't work in settings
once DEPRECATED("Method 'Baggy.exists'","the :exists adverb");
self.exists_key($k);
Expand All @@ -14,8 +15,8 @@ my role Baggy does QuantHash {
%!elems.exists_key($k.WHICH);
}
method Bool { %!elems.Bool }
method Numeric { self.elems }
method Real { self.elems }
method Numeric { self.total }
method Real { self.total }

method hash(--> Hash) { %!elems.values.hash }
method invert(--> List) { %!elems.values.map: { ( .value => .key ) } }
Expand Down Expand Up @@ -76,33 +77,33 @@ my role Baggy does QuantHash {
method pick ($count = 1) {
return self.roll if $count ~~ Num && $count == 1;

my $elems = self.elems;
my $picks = $elems min $count;
my $total = self.total;
my $picks = $total min $count;
my @pairs = self.pairs.map( { $_.key => $_.value } );;

map {
my $rand = $elems.rand.Int;
my $rand = $total.rand.Int;
my $seen = 0;
my $pick;
for @pairs -> $pair {
next if ( $seen += $pair.value ) <= $rand;

$pick = $pair.key;
$pair.value--;
$elems--;
$total--;
last;
}
$pick;
}, 1 .. $picks;
}

method roll ($count = 1) {
my $elems = self.elems;
my $rolls = $count ~~ Num ?? $elems min $count !! $count;
my $total = self.total;
my $rolls = $count ~~ Num ?? $total min $count !! $count;
my @pairs := self.pairs;

map {
my $rand = $elems.rand.Int;
my $rand = $total.rand.Int;
my $seen = 0;
my $roll;
for @pairs -> $pair {
Expand Down
1 change: 1 addition & 0 deletions src/core/Setty.pm
Expand Up @@ -6,6 +6,7 @@ my role Setty does QuantHash {
method keys { %!elems.values }
method values { True xx %!elems.elems }
method elems(--> Int) { %!elems.elems }
method total(--> Int) { %!elems.elems }
method exists ($k --> Bool) { # is DEPRECATED doesn't work in settings
once DEPRECATED("Method 'Setty.exists'","the :exists adverb");
self.exists_key($k);
Expand Down

0 comments on commit 0dbbbfe

Please sign in to comment.