File tree Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -13,7 +13,29 @@ L<Mixy>.
13
13
14
14
= head2 method grab
15
15
16
- = comment TODO
16
+ Defined as:
17
+
18
+ multi method grab(Baggy:D:) returns Any
19
+ multi method grab(Baggy:D: $count) returns Array:D
20
+
21
+ Like L < pick|#method pick > , a C < grab > returns a random selection of elements, weighted
22
+ by the values corresponding to each key. Unlike C < pick > , it works only on mutable
23
+ structures, e.g. L < BagHash|/type/BagHash > . Use of C < grab > on an immutable structure results in an
24
+ C < X::Immutable > exception. If C < * > is passed as C < $count > , or C < $count > is greater than
25
+ or equal to the L < total|#method total > of the invocant, then C < total > elements from the
26
+ invocant are returned in a random sequence.
27
+
28
+ Grabbing decrements the grabbed key's weight by one (deleting the key
29
+ when it reaches 0). By definition, the C < total > of the invocant also decreases by one, so the
30
+ probabilities stay consistent through subsequent C < grab > operations.
31
+
32
+ my $cars = ('Ford' => 2, 'Rover' => 3).BagHash;
33
+ say $cars.grab; # Ford
34
+ say $cars.grab(2); # [Rover Rover]
35
+ say $cars.grab(*); # [Rover Ford]
36
+
37
+ my $breakfast = ('eggs' => 2, 'bacon' => 3).Bag;
38
+ say $breakfast.grab; # throws X::Immutable exception
17
39
18
40
= head2 method grabpairs
19
41
You can’t perform that action at this time.
0 commit comments