Skip to content

Instantly share code, notes, and snippets.

@dogbert17
Created June 16, 2016 19:17
Show Gist options
  • Select an option

  • Save dogbert17/0f5a38acd1c385629d604dd480398b82 to your computer and use it in GitHub Desktop.

Select an option

Save dogbert17/0f5a38acd1c385629d604dd480398b82 to your computer and use it in GitHub Desktop.
Attempt to document method grabpairs in role Baggy
=head2 method grabpairs
Defined as:
multi method grabpairs(Baggy:D:) returns Any
multi method grabpairs(Baggy:D: $count) returns List:D
Returns a C<Pair> or a C<List> of C<Pair>s depending on the version of the method
being invoked. Each C<Pair> returned has an element of the invocant as its key and the
elements weight as its value. Unlike L<pickpairs>, it works only on mutable structures,
e.g. L<BagHash|/type/BagHash>. Use of C<grabpairs> on an immutable structure results in
an C<X::Immutable> exception. If C<*> is passed as C<$count>, or C<$count> is greater
than or equal to the number of L<elements|#method elems> of the invocant, then all
element/weight C<Pair>s from the invocant are returned in a random sequence.
What makes C<grabpairs> different from L<pickpairs|#method pickpairs> is that the
'grabbed' elements are in fact removed from the invocant.
my $breakfast = (eggs => 2, bacon => 3).BagHash;
say $breakfast.grabpairs; # bacon => 3
say $breakfast.grabpairs(1); # (eggs => 2)
say $breakfast.grabpairs(*); # []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment