Skip to content
Browse files

Initial implementation of .grab for Set/Bag/Mix

  • Loading branch information...
1 parent 61580f2 commit 01c2e2a92ce472c4e20f0c24a7a7f8636e15694a @lizmat lizmat committed Oct 4, 2013
Showing with 26 additions and 8 deletions.
  1. +4 −1 src/core/Bag.pm
  2. +9 −5 src/core/Baggy.pm
  3. +4 −1 src/core/Mix.pm
  4. +4 −0 src/core/Mixy.pm
  5. +4 −1 src/core/Set.pm
  6. +1 −0 src/core/Setty.pm
View
5 src/core/Bag.pm
@@ -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 }
View
14 src/core/Baggy.pm
@@ -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;
View
5 src/core/Mix.pm
@@ -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 }
View
4 src/core/Mixy.pm
@@ -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}";
}
View
5 src/core/Set.pm
@@ -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 }
View
1 src/core/Setty.pm
@@ -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) }

0 comments on commit 01c2e2a

Please sign in to comment.
Something went wrong with that request. Please try again.