Permalink
Browse files

Implement KeyBag.push

  • Loading branch information...
1 parent bc379f6 commit 71afa3b5e994bfc320f8ac1b9acb23c155c3ec58 @lizmat lizmat committed Aug 3, 2013
Showing with 22 additions and 0 deletions.
  1. +22 −0 src/core/KeyBag.pm
View
22 src/core/KeyBag.pm
@@ -46,6 +46,28 @@ my class KeyBag does Associative does Baggy {
method list() { %!elems.keys }
method pairs() { %!elems.pairs }
+ method push(*@values) {
+ my $previous;
+ my $has_previous;
+ for @values -> $e {
+ if $has_previous {
+ self{$previous} += $e;
+ $has_previous = 0;
+ }
+ elsif $e.^isa(Enum) {
+ self{$e.key} += $e.value;
+ }
+ else {
+ $previous = $e;
+ $has_previous = 1;
+ }
+ }
+ if $has_previous {
+ warn "Trailing item in KeyBag.push";
+ }
+ self
+ }
+
method pick($count = 1) {
return self.roll if $count ~~ Num && $count == 1;

2 comments on commit 71afa3b

@colomon

I really don't like the idea that push takes arguments two at a time, making the second one the count. That's not how any other interface for specifying elements to a bag works.

I definitely like the idea of having push, though. lizmat++

@lizmat
Please sign in to comment.