See Original text in context
multi method grabpairs(Baggy: --> Any)multi method grabpairs(Baggy: --> Seq)
Returns a Pair
or a Seq
of Pair
s depending on the version of the method being invoked. Each Pair
returned has an element of the invocant as its key and the element's weight as its value. Unlike pickpairs, it works only on mutable structures, e.g. BagHash. Use of grabpairs
on an immutable structure results in an X::Immutable
exception. If *
is passed as $count
, or $count
is greater than or equal to the number of elements of the invocant, then all element/weight Pair
s from the invocant are returned in a random sequence.
What makes grabpairs
different from pickpairs is that the 'grabbed' elements are in fact removed from the invocant.
my = (eggs => 2, bacon => 3).BagHash;say .grabpairs; # OUTPUT: «bacon => 3»say ; # OUTPUT: «BagHash.new(eggs(2))»say .grabpairs(1); # OUTPUT: «(eggs => 2)»say .grabpairs(*); # OUTPUT: «()»my = ('eggs' => 2, 'bacon' => 3).Bag;say .grabpairs;CATCH ;# OUTPUT: «X::Immutable: Cannot call 'grabpairs' on an immutable 'Bag'»
See Original text in context
method grabpairs( = 1)
Removes $count
elements chosen at random (without repetition) from the set, and returns a list of Pair
objects whose keys are the grabbed elements and whose values are True
.
If *
is passed as $count
, or $count
is greater than or equal to the size of the set, then all its elements are removed and returned as Pair
s in the aforementioned way in random order.
Only works on mutable sets; When used on an immutable set, it results in an exception.