does QuantHash
A role for collections of weighted objects. See Bag, BagHash, and Mixy.
Methods §
method new-from-pairs §
Defined as:
method new-from-pairs(Baggy: * --> Baggy)
Constructs a Baggy objects from a list of Pair
objects given as positional arguments:
say Mix.new-from-pairs: 'butter' => 0.22, 'sugar' => 0.1, 'sugar' => 0.02;# OUTPUT: «Mix(butter(0.22) sugar(0.12))»
Note: be sure you aren't accidentally passing the Pairs as positional arguments; the quotes around the keys in the above example are significant.
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'»
method grabpairs §
Defined as:
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'»
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)»
method pickpairs §
Defined as:
multi method pickpairs(Baggy: --> Pair)multi method pickpairs(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. The elements are 'picked' without replacement. 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.
Note that each pickpairs
invocation maintains its own private state and has no effect on subsequent pickpairs
invocations.
my = bag <eggs bacon bacon bacon>;say .pickpairs; # OUTPUT: «eggs => 1» say .pickpairs(1); # OUTPUT: «(bacon => 3)» say .pickpairs(*); # OUTPUT: «(eggs => 1 bacon => 3)»
method roll §
Defined as:
multi method roll(Baggy: --> Any)multi method roll(Baggy: --> Seq)
Like an ordinary list roll, 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 roll used. The underlying metaphor for rolling is that you're throwing $count
dice that are independent of each other, which (in bag terms) is equivalent to picking a colored marble out your bag and then putting it back, and doing this $count
times. In dice terms, the number of marbles corresponds to the number of sides, and the number of marbles of the same color corresponds to the number of sides with the same color. (For "picking without replacement" see pick instead).
If *
is passed to $count
, returns a lazy, infinite sequence of randomly chosen elements from the invocant.
my = bag <eggs bacon bacon bacon>;say .roll; # OUTPUT: «bacon» say .roll(3); # OUTPUT: «(bacon eggs bacon)» my := .roll(*);say [^5]; # OUTPUT: «(bacon eggs bacon bacon bacon)»
method pairs §
Defined as:
method pairs(Baggy: --> Seq)
Returns all elements and their respective weights as a Seq of Pair
s where the key is the element itself and the value is the weight of that element.
my = bag <bacon eggs bacon>;my = .pairs;say .sort; # OUTPUT: «(bacon => 2 eggs => 1)»
method antipairs §
Defined as:
method antipairs(Baggy: --> Seq)
Returns all elements and their respective weights as a Seq of Pairs, where the element itself is the value and the weight of that element is the key, i.e. the opposite of method pairs.
my = bag <bacon eggs bacon>;my = .antipairs;say .sort; # OUTPUT: «(1 => eggs 2 => bacon)»
method invert §
Defined as:
method invert(Baggy: --> Seq)
Returns all elements and their respective weights as a Seq of Pairs, where the element itself is the value and the weight of that element is the key, i.e. the opposite of method pairs. Except for some esoteric cases, invert
on a Baggy type returns the same result as antipairs.
my = bag <bacon eggs bacon>;my = .invert;say .sort; # OUTPUT: «(1 => eggs 2 => bacon)»
method classify-list §
Defined as:
multi method classify-list(, * --> Baggy)multi method classify-list(, * --> Baggy)multi method classify-list(, * --> Baggy)
Populates a mutable Baggy
by classifying the possibly-empty @list
of values using the given mapper
. The @list
cannot be lazy.
say BagHash.new.classify-list: , ^10;# OUTPUT: BagHash(even(5) odd(5)) my = <zero one two three four five>;say MixHash.new.classify-list: , 1, 2, 3, 4, 4, 6;# OUTPUT: MixHash((Any) two three four(2) one)
The mapper can be a Callable
that takes a single argument, an Associative
, or an Iterable
. With Associative
and an Iterable
mappers, the values in the @list
represent the key and index of the mapper's value respectively. A Callable
mapper will be executed once per each item in the @list
, with that item as the argument and its return value will be used as the mapper's value.
The mapper's value is used as the key of the Baggy
that will be incremented by 1
. See .categorize-list
if you wish to classify an item into multiple categories at once.
Note: unlike the Hash
's .classify-list
, returning an Iterable
mapper's value will throw, as Baggy
types do not support nested classification. For the same reason, Baggy
's .classify-list
does not accept :&as
parameter.
method categorize-list §
Defined as:
multi method categorize-list(, * --> Baggy)multi method categorize-list(, * --> Baggy)multi method categorize-list(, * --> Baggy)
Populates a mutable Baggy
by categorizing the possibly-empty @list
of values using the given mapper
. The @list
cannot be lazy.
say BagHash.new.categorize-list: , ^10;# OUTPUT: BagHash(largish(4) even(5) non-prime(6) prime(4) odd(5)) my = :sugar<sweet white>, :lemon<sour>, :cake('sweet', 'is-a-lie');say MixHash.new.categorize-list: , <sugar lemon cake>;# OUTPUT: MixHash(is-a-lie sour white sweet(2))
The mapper can be a Callable
that takes a single argument, an Associative
, or an Iterable
. With Associative
and an Iterable
mappers, the values in the @list
represent the key and index of the mapper's value respectively. A Callable
mapper will be executed once per each item in the @list
, with that item as the argument and its return value will be used as the mapper's value.
The mapper's value is used as a possibly-empty list of keys of the Baggy
that will be incremented by 1
.
Note: unlike the Hash
's .categorize-list
, returning a list of Iterables
as mapper's value will throw, as Baggy
types do not support nested categorization. For the same reason, Baggy
's .categorize-list
does not accept :&as
parameter.
method keys §
Defined as:
method keys(Baggy: --> Seq)
Returns a Seq
of all keys in the Baggy
object without taking their individual weights into account as opposed to kxxv.
my = bag <eggs spam spam spam>;say .keys.sort; # OUTPUT: «(eggs spam)» my = ("a" => 5, "b" => 2).BagHash;say .keys.sort; # OUTPUT: «(a b)»
method values §
Defined as:
method values(Baggy: --> Seq)
Returns a Seq
of all values, i.e. weights, in the Baggy
object.
my = bag <eggs spam spam spam>;say .values.sort; # OUTPUT: «(1 3)» my = ("a" => 5, "b" => 2, "a" => 1).BagHash;say .values.sort; # OUTPUT: «(2 6)»
method kv §
Defined as:
method kv(Baggy: --> Seq)
Returns a Seq
of keys and values interleaved.
my = bag <eggs spam spam spam>;say .kv; # OUTPUT: «(spam 3 eggs 1)» my = ("a" => 5, "b" => 2, "a" => 1).BagHash;say .kv; # OUTPUT: «(a 6 b 2)»
method kxxv §
Defined as:
method kxxv(Baggy: --> Seq)
Returns a Seq
of the keys of the invocant, with each key multiplied by its weight. Note that kxxv
only works for Baggy
types which have integer weights, i.e. Bag and BagHash.
my = bag <spam eggs spam spam bacon>;say .kxxv.sort; # OUTPUT: «(bacon eggs spam spam spam)» my = ("a" => 0, "b" => 1, "b" => 2).BagHash;say .kxxv; # OUTPUT: «(b b b)»
method elems §
Defined as:
method elems(Baggy: --> Int)
Returns the number of elements in the Baggy
object without taking the individual elements' weight into account.
my = bag <eggs spam spam spam>;say .elems; # OUTPUT: «2» my = ("b" => 9.4, "b" => 2).MixHash;say .elems; # OUTPUT: «1»
method total §
Defined as:
method total(Baggy:)
Returns the sum of weights for all elements in the Baggy
object.
my = bag <eggs spam spam bacon>;say .total; # OUTPUT: «4» my = ("a" => 5, "b" => 1, "b" => 2).BagHash;say .total; # OUTPUT: «8»
method default §
Defined as:
method default(Baggy: --> 0)
Returns zero.
my = bag <eggs bacon>;say .default; # OUTPUT: «0»
method hash §
Defined as:
method hash(Baggy: --> Hash)
Returns a Hash where the elements of the invocant are the keys and their respective weights the values.
my = bag <eggs bacon bacon>;my = .hash;say .^name; # OUTPUT: «Hash[Any,Any]» say ; # OUTPUT: «{bacon => 2, eggs => 1}»
method Bool §
Defined as:
method Bool(Baggy: --> Bool)
Returns True
if the invocant contains at least one element.
my = ('eggs' => 1).BagHash;say .Bool; # OUTPUT: «True» # (since we have one element) <eggs> = 0; # weight == 0 will lead to element removal say .Bool; # OUTPUT: «False»
method Set §
Defined as:
method Set(--> Set)
Returns a Set whose elements are the keys of the invocant.
my = (eggs => 2, bacon => 3).BagHash;say .Set; # OUTPUT: «Set(bacon eggs)»
method SetHash §
Defined as:
method SetHash(--> SetHash)
Returns a SetHash whose elements are the keys of the invocant.
my = (eggs => 2, bacon => 3).BagHash;my = .SetHash;say .^name; # OUTPUT: «SetHash» say .elems; # OUTPUT: «2»
method ACCEPTS §
Defined as:
method ACCEPTS( --> Bool)
Used in smartmatching if the right-hand side is a Baggy
.
If the right-hand side is the type object, i.e. Baggy
, the method returns True
if $other
does Baggy
otherwise False
is returned.
If the right-hand side is a Baggy
object, True
is returned only if $other
has the same elements, with the same weights, as the invocant.
my = bag <eggs bacon>;say ~~ Baggy; # OUTPUT: «True» say .does(Baggy); # OUTPUT: «True» my = (eggs => 1, bacon => 1).Mix;say ~~ ; # OUTPUT: «True» my = (eggs => 1, bacon => 2).Bag;say ~~ ; # OUTPUT: «False»
See Also §
Type Graph §
Routines supplied by role QuantHash §
Baggy does role QuantHash, which provides the following routines:
(QuantHash) method hash §
method hash()
Coerces the QuantHash
object to a Hash (by stringifying the objects for the keys) with the values of the hash limited to the same limitation as QuantHash
, and returns that.
(QuantHash) method Hash §
method Hash()
Coerces the QuantHash
object to a Hash (by stringifying the objects for the keys) without any limitations on the values, and returns that.
(QuantHash) method of §
method of()
Returns the type of value a value of this QuantHash
may have. This is typically Bool for Setty, UInt for Baggy or Real for Mixy roles.
(QuantHash) method keyof §
method keyof()
Returns the type of value a key of this subclass of QuantHash
may have. This is typically Mu, which is also the default for punned QuantHashes.
(QuantHash) method Capture §
Defined as
method Capture()
Returns the object as a Capture
by previously coercing it to a Hash
.
(QuantHash) method list §
Defined as:
multi method list(QuantHash:)
Returns a list of Pair objects of all keys and values in the QuantHash.
(QuantHash) method Setty §
method Setty(--> Setty)
Coerce the QuantHash
object to the equivalent object that uses the Setty role. Note that for Mixy type coercion items with negative values will be skipped.
my is Bag = one => 1, two => 2;say .Setty; # OUTPUT: «Set(one two)» my is Mix = one => 1, minus => -1;say .Setty; # OUTPUT: «Set(one)»
(QuantHash) method Baggy §
method Baggy(--> Baggy)
Coerce the QuantHash
object to the equivalent object that uses the Baggy role. Note that for Mixy type coercion items with negative values will be skipped.
my is Set = <one two>;say .Baggy; # OUTPUT: «Bag(one two)» my is Mix = one => 1, minus => -1;say .Baggy; # OUTPUT: «Bag(one)»
(QuantHash) method Mixy §
method Mixy(--> Mixy)
Coerce the QuantHash
object to the equivalent object that uses the Mixy role.
my is Set = <one two>;say .Mixy; # OUTPUT: «Mix(one two)» my is Bag = one => 1, two => 2;say .Mixy; # OUTPUT: «Mix(one two(2))»
Routines supplied by role Associative §
Baggy does role Associative, which provides the following routines:
(Associative) method of §
Defined as:
method of()
Associative
, as the definition above shows, is actually a parameterized role which can use different classes for keys and values. As seen at the top of the document, by default it coerces the key to Str
and uses a very generic Mu
for value.
my ;say .of; # OUTPUT: «(Mu)»
The value is the first parameter you use when instantiating Associative
with particular classes:
is Hash does Associative[Cool,DateTime] ;my := DateHash.new;say .of; # OUTPUT: «(Cool)»
(Associative) method keyof §
Defined as:
method keyof()
Returns the parameterized key used for the Associative role, which is Any
coerced to Str
by default. This is the class used as second parameter when you use the parameterized version of Associative.
my ;.keyof; # OUTPUT: «(Str(Any))»
(Associative) method AT-KEY §
method AT-KEY(\key)
Should return the value / container at the given key.
;say What.new; # OUTPUT: «42»
(Associative) method EXISTS-KEY §
method EXISTS-KEY(\key)
Should return a Bool
indicating whether the given key actually has a value.
(Associative) method STORE §
method STORE(\values, :)
This method should only be supplied if you want to support the:
my is Foo = a => 42, b => 666;
syntax for binding your implementation of the Associative
role.
Should accept the values to (re-)initialize the object with, which either could consist of Pair
s, or separate key/value pairs. The optional named parameter will contain a True
value when the method is called on the object for the first time. Should return the invocant.