Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Initial implementation of .grab for Set/Bag/Mix
  • Loading branch information
lizmat committed Oct 4, 2013
1 parent 61580f2 commit 01c2e2a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/core/Bag.pm
Expand Up @@ -23,7 +23,10 @@ my class Bag does Baggy {
self.delete_key($a);
}
method delete_key($a --> Int) is hidden_from_backtrace {
X::Immutable.new( method => 'delete', typename => self.^name ).throw;
X::Immutable.new( method => 'delete_key', typename => self.^name ).throw;
}
method grab($count = 1 --> Int) is hidden_from_backtrace {
X::Immutable.new( method => 'grab', typename => self.^name ).throw;
}

method Bag { self }
Expand Down
14 changes: 9 additions & 5 deletions src/core/Baggy.pm
Expand Up @@ -76,12 +76,16 @@ my role Baggy does QuantHash {
method list() { self.keys }
method pairs() { %!elems.values }

method pick ($count = 1) {
return self.roll if $count ~~ Num && $count == 1;
method grab ($count = 1) { self.pick($count, :BIND) }

my $total = self.total;
my $picks = $total min $count;
my @pairs = %!elems.values.map( { $_.key => $_.value } );
method pick ($count = 1, :$BIND) {
return self.roll if $count ~~ Num && $count == 1 && !$BIND;

my $total = self.total;
my $picks = $total min $count;
my @pairs := $BIND
?? %!elems.values
!! %!elems.values.map( { .key => .value } );

map {
my $rand = $total.rand.Int;
Expand Down
5 changes: 4 additions & 1 deletion src/core/Mix.pm
Expand Up @@ -14,7 +14,10 @@ my class Mix does Mixy {
self.delete_key($a);
}
method delete_key($a --> Real) is hidden_from_backtrace {
X::Immutable.new( method => 'delete', typename => self.^name ).throw;
X::Immutable.new( method => 'delete_key', typename => self.^name ).throw;
}
method grab($count = 1 --> Real) is hidden_from_backtrace {
X::Immutable.new( method => 'grab', typename => self.^name ).throw;
}

method Mix { self }
Expand Down
4 changes: 4 additions & 0 deletions src/core/Mixy.pm
Expand Up @@ -14,6 +14,10 @@ my role Mixy does Baggy { # should really be QuantHash, but that's for later
~ ')';
}

method grab ($count = 1) {
fail ".grab is not supported on a {.self.^name}";
}

method pick ($count = 1) {
fail ".pick is not supported on a {.self.^name}";
}
Expand Down
5 changes: 4 additions & 1 deletion src/core/Set.pm
Expand Up @@ -17,7 +17,10 @@ my class Set does Setty {
self.delete_key($a);
}
method delete_key($k --> Bool) is hidden_from_backtrace {
X::Immutable.new( method => 'delete', typename => self.^name ).throw;
X::Immutable.new( method => 'delete_key', typename => self.^name ).throw;
}
method grab ($count = 1) {
X::Immutable.new( method => 'grab', typename => self.^name ).throw;
}

method Set { self }
Expand Down
1 change: 1 addition & 0 deletions src/core/Setty.pm
Expand Up @@ -66,6 +66,7 @@ my role Setty does QuantHash {

method list() { %!elems.values }
method pairs() { %!elems.values.map({ $_ => True }) }
method grab($count = 1) { %!elems{ %!elems.keys.pick($count) }:delete }
method pick($count = 1) { %!elems.values.pick($count) }
method roll($count = 1) { %!elems.values.roll($count) }

Expand Down

0 comments on commit 01c2e2a

Please sign in to comment.