Skip to content

Commit 01e959b

Browse files
author
Jan-Olof Hendig
committed
Added docs for Baggy.grab
1 parent fad1877 commit 01e959b

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

doc/Type/Baggy.pod

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,29 @@ L<Mixy>.
1313
1414
=head2 method grab
1515
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
1739
1840
=head2 method grabpairs
1941

0 commit comments

Comments
 (0)