Documentation for routine pick
assembled from the following pages:
Role: Baggy §
From Baggy
(Baggy) method pick §
Defined as:
multi method pick(Baggy: --> Any)multi method pick(Baggy: --> Seq)
Like an ordinary list pick, but returns keys of the invocant weighted by their values, as if the keys were replicated the number of times indicated by the corresponding value and then list pick used. The underlying metaphor for picking is that you're pulling colored marbles out a bag. (For "picking with replacement" see roll instead). 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.
Note that each pick
invocation maintains its own private state and has no effect on subsequent pick
invocations.
my = bag <eggs bacon bacon bacon>;say .pick; # OUTPUT: «eggs» say .pick(2); # OUTPUT: «(eggs bacon)» say .total; # OUTPUT: «4» say .pick(*); # OUTPUT: «(bacon bacon bacon eggs)»
Class: Any §
From Any
(Any) method pick §
Defined as:
multi method pick(--> Any)multi method pick( --> Seq)
Coerces the invocant to a list
by applying its .list
method and uses List.pick
on it.
my Range = 'α'..'ω';say .pick(3); # OUTPUT: «(β α σ)»
Class: List §
From List
(List) routine pick §
Defined as:
multi sub pick(, * --> Seq)multi method pick(List: --> Seq)multi method pick(List: --> Mu)
If $count
is supplied: Returns $count
elements chosen at random and without repetition from the invocant. If *
is passed as $count
, or $count
is greater than or equal to the size of the list, then all elements from the invocant list are returned in a random sequence; i.e. they are returned shuffled.
In method form, if $count
is omitted: Returns a single random item from the list, or Nil if the list is empty
Examples:
say <a b c d e>.pick; # OUTPUT: «b» say <a b c d e>.pick: 3; # OUTPUT: «(c a e)» say <a b c d e>.pick: *; # OUTPUT: «(e d a b c)»
Role: Setty §
From Setty
(Setty) method pick §
multi method pick( = 1)
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 returned in random order (shuffled).
Enum: Bool §
From Bool
(Bool) routine pick §
multi method pick(Bool --> Bool)multi method pick(Bool --> Seq)
Returns True
or False
if called without any argument. Otherwise returns $count
elements chosen at random (without repetition) from the enum
. If *
is passed as $count
, or $count
is greater than or equal to two, then both elements are returned in random order.
say Bool.pick; # OUTPUT: «True» say Bool.pick(1); # OUTPUT: «(False)» say Bool.pick(*); # OUTPUT: «(False True)»
Role: Enumeration §
From Enumeration
(Enumeration) method pick §
Defined as:
multi method pick(::?CLASS:)multi method pick(::?CLASS: \n)multi method pick(::?CLASS: *)
It works on the defined class, selecting one element and eliminating it.
say Norse-gods.pick() for ^3; # OUTPUT: «ÞorFreijaOðin»
Class: Range §
From Range
(Range) method pick §
multi method pick(Range: --> Any)multi method pick(Range: --> Seq)
Performs the same function as Range.list.pick
, but attempts to optimize by not actually generating the list if it is not necessary.