Documentation for method
grab assembled from the following types:
multi method grab(Baggy: --> Any)multi method grab(Baggy: --> Seq)
Like pick, a
grab returns a random selection of elements, weighted by the values corresponding to each key. Unlike
pick, it works only on mutable structures, e.g. BagHash. Use of
grab on an immutable structure results in an
X::Immutable exception. If
* is passed as
$count is greater than or equal to the total of the invocant, then
total elements from the invocant are returned in a random sequence; i.e. they are returned shuffled.
Grabbing decrements the grabbed key's weight by one (deleting the key when it reaches 0). By definition, the
total of the invocant also decreases by one, so the probabilities stay consistent through subsequent
my = ('Ford' => 2, 'Rover' => 3).BagHash;say .grab; # OUTPUT: «Ford␤»say .grab(2); # OUTPUT: «(Rover Rover)␤»say .grab(*); # OUTPUT: «(Rover Ford)␤»my = ('eggs' => 2, 'bacon' => 3).Bag;say .grab;CATCH ;# OUTPUT: «X::Immutable: Cannot call 'grab' on an immutable 'Bag'␤»
method grab( = 1)
Removes and returns
$count elements chosen at random (without repetition) from the set.
* is passed as
$count is greater than or equal to the size of the set, then all its elements are removed and returned in random order.
Only works on mutable sets; When used on an immutable set, it results in an exception.
method grab(Supply: --> Supply)
Supply it is called on. When it is
&when-done and then emits the list of values that it returns on the result
Supply. If the original
quits, then the exception is immediately conveyed on the return
my = Supply.from-list(4, 10, 3, 2);my = .grab();.tap(); # OUTPUT: «19␤»