method grab
Documentation for method grab
assembled from the following types:
role Baggy
From Baggy
(Baggy) method grab
Defined as:
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
, or $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 grab
operations.
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'»
role Setty
From Setty
(Setty) method grab
method grab( = 1)
Removes and returns $count
elements chosen at random (without repetition) from the set.
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 in random order.
Only works on mutable sets; When used on an immutable set, it results in an exception.
class Supply
From Supply
(Supply) method grab
method grab(Supply: --> Supply)
Taps the Supply
it is called on. When it is done
, calls &when-done
and then emits the list of values that it returns on the result Supply
. If the original Supply
quit
s, then the exception is immediately conveyed on the return Supply
.
my = Supply.from-list(4, 10, 3, 2);my = .grab();.tap(); # OUTPUT: «19»