class BagHash does Baggy { }

A BagHash is a mutable bag/multiset, meaning a collection of distinct items in no particular order that each have an integer weight assigned to them signifying how many copies of that element are considered "in the bag". If you do not need the mutability that a BagHash provides, consider using the immutable Bag type instead.

An item may be a definite object of any type – not just a Str. For example, you can store Sub's in a BagHash, and you will store the actual Sub rather than a string with the same name as the Sub. Within a BagHash, items that would compare positively with the === operator are considered the same element, with the number of how many there were as its weight. Alternatively, you can use the kxxv method to easily get back the expanded list of items (without the order):

my $breakfast = <spam eggs spam spam bacon spam>.BagHash;
 
say $breakfast.elems;      # OUTPUT: «3␤» 
say $breakfast.keys.sort;  # OUTPUT: «bacon eggs spam␤» 
 
say $breakfast.total;      # OUTPUT: «6␤» 
say $breakfast.kxxv.sort;  # OUTPUT: «bacon eggs spam spam spam spam␤» 

BagHashes can be treated as object hashes using the { } postcircumfix operator, or the < > postcircumfix operator for literal string keys, which returns the corresponding integer weight for keys that are elements of the bag, and 0 for keys that aren't. These operators can also be used to modify weights (see Updating BagHash Objects, below).

my $breakfast = <spam eggs spam spam bacon spam>.BagHash;
say $breakfast<bacon>;     # OUTPUT: «1␤» 
say $breakfast<spam>;      # OUTPUT: «4␤» 
say $breakfast<sausage>;   # OUTPUT: «0␤» 
 
$breakfast<sausage> = 2;
$breakfast<bacon>--;
say $breakfast.kxxv.sort;  # OUTPUT: «eggs sausage sausage spam spam spam spam␤» 

Creating BagHash objects §

BagHashes can be composed using BagHash.new. Any positional parameters, regardless of their type, become elements of the bag:

my $n = BagHash.new: "a""b""c""c";
say $n.raku;             # OUTPUT: «("b"=>1,"a"=>1,"c"=>2).BagHash␤» 
say $n.keys.raku;        # OUTPUT: «("b", "a", "c").Seq␤» 
say $n.values.raku;      # OUTPUT: «(1, 1, 2).Seq␤»

Besides, BagHash.new-from-pairs can create a BagHash with items and their occurrences.

my $n = BagHash.new-from-pairs: "a" => 0"b" => 1"c" => 2"c" => 2;
say $n.raku;             # OUTPUT: «("b"=>1,"c"=>4).BagHash␤» 
say $n.keys.raku;        # OUTPUT: «("b", "c").Seq␤» 
say $n.values.raku;      # OUTPUT: «(1, 4).Seq␤»

Alternatively, the .BagHash coercer (or its functional form, BagHash()) can be called on an existing object to coerce it to a BagHash. Its semantics depend on the type and contents of the object. In general it evaluates the object in list context and creates a bag with the resulting items as elements, although for Hash-like objects or Pair items, only the keys become elements of the bag, and the (cumulative) values become the associated integer weights:

my $m = ("a""b""c""c").BagHash;
my $n = ("a" => 0"b" => 1"c" => 2"c" => 2).BagHash;
say $m.raku;             # OUTPUT: «("b"=>1,"a"=>1,"c"=>2).BagHash␤» 
say $n.raku;             # OUTPUT: «("b"=>1,"c"=>4).BagHash␤»

You can also create BagHash masquerading as a hash by using the is trait:

my %bh is BagHash = <a b b c c c>;
say %bh<b>;  # 2 
say %bh<d>;  # 0

Since 6.d (2019.03 and later) it is also possible to specify the type of values you would like to allow in a BagHash. This can either be done when calling .new:

# only allow strings 
my $n = BagHash[Str].new: <a b b c c c>;

or using the masquerading syntax:

# only allow strings 
my %bh is BagHash[Str= <a b b c c c>;
say %bh<b>;  # 2 
say %bh<d>;  # 0 
 
# only allow whole numbers 
my %bh is BagHash[Int= <a b b c c c>;
# Type check failed in binding; expected Int but got Str ("a")

Updating BagHash Objects §

Once you have created a BagHash, you can update its values in two ways. First, you can use the add and remove methods:

my $n = BagHash.new: "a""b""c""c";
say $n.raku;             # OUTPUT: «("b"=>1,"a"=>1,"c"=>2).BagHash␤» 
$n.add('c');
say $n.raku;             # OUTPUT: «("b"=>1,"c"=>3,"a"=>1).BagHash␤» 
$n.remove(('b''a'),);
say $n.raku;             # OUTPUT: «("c"=>3).BagHash␤» 
$n.remove('c');
say $n.raku;             # OUTPUT: «("c"=>2).BagHash␤»

Note that, as shown in the final example, the remove method removes a single value from the BagHash; it doesn't entirely remove the key from the BagHash.

Alternatively, you can use assignment (including with autoincrement operators such as ++ and --) to modify the BagHash's contents.

my $n = BagHash.new: "a""b""c""c";
say $n.raku;             # OUTPUT: «("b"=>1,"a"=>1,"c"=>2).BagHash␤» 
$n<c>++;
say $n.raku;             # OUTPUT: «("b"=>1,"c"=>3,"a"=>1).BagHash␤» 
$n<b> -= 1;
say $n.raku;             # OUTPUT: «("a"=>1,"c"=>3).BagHash␤» 
$n{'a'} = 0;
say $n.raku;             # OUTPUT: «("c"=>3).BagHash␤»

Using either syntax, if you set the value of an item to zero or less than zero, the item will be removed from the BagHash.

Operators §

See Operators with set semantics for a complete list of "set operators" applicable to, among other types, BagHash.

Examples:

my ($a$b= BagHash.new(224), BagHash.new(2334);
 
say $a (<) $b;   # OUTPUT: «False␤» 
say $a (<=) $b;  # OUTPUT: «False␤» 
say $a (^) $b;   # OUTPUT: «BagHash(3(2) 2)␤» 
say $a (+) $b;   # OUTPUT: «BagHash(2(3) 4(2) 3(2))␤» 
 
# Unicode versions: 
say $a  $b;  # OUTPUT: «False␤» 
say $a  $b;  # OUTPUT: «False␤» 
say $a  $b;  # OUTPUT: «BagHash(3(2) 2)␤» 
say $a  $b;  # OUTPUT: «BagHash(2(3) 4(2) 3(2))␤» 

Note on reverse and ordering. §

BagHash inherits reverse from Any, however, Bags do not have an inherent order and you should not trust it returning a consistent output.

If you sort a BagHash, the result is a list of pairs, at which point reverse makes perfect sense:

my $a = BagHash.new(221834);
say $a;  # OUTPUT: «BagHash(18 2(2) 3 4)␤» 
 
say $a.sort;  # OUTPUT: «(2 => 2 3 => 1 4 => 1 18 => 1)␤» 
say $a.sort.reverse;  # OUTPUT: «(18 => 1 4 => 1 3 => 1 2 => 2)␤» 

method add §

method add(BagHash: \to-add*%_ --> Nil)

When to-add is a single item, add inserts it into the BagHash or, if it was already present, increases its weight by 1. When to-add is a List, Array, Seq, or any other type that does the Iterator Role, add inserts each element of the Iterator into the SetHash or increments the weight of each element by 1.

Note: Added in version 2020.02.

method remove §

method remove(BagHash: \to-remove*%_ --> Nil)

When to-remove is a single item, remove reduces the weight of that item by one. If this results in the item having a weight of 0, this removes the item from the BagHash. If the item is not present in the BagHash, remove has no effect. When to-remove is a List, Array, Seq, or any other type that does the Iterator Role, remove reduces the weight of each element by 1 and removes any items with the resulting weight of 0.

Note: Added in version 2020.02.

See Also §

Sets, Bags, and Mixes

Type Graph §

Type relations for BagHash
perl6-type-graph BagHash BagHash Any Any BagHash->Any Baggy Baggy BagHash->Baggy Mu Mu Any->Mu Associative Associative QuantHash QuantHash QuantHash->Associative Baggy->QuantHash

Expand above chart

Routines supplied by role Baggy §

BagHash does role Baggy, which provides the following routines:

(Baggy) method new-from-pairs §

Defined as:

method new-from-pairs(Baggy: *@pairs --> Baggy:D)

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.

(Baggy) method grab §

Defined as:

multi method grab(Baggy:D: --> Any)
multi method grab(Baggy:D: $count --> Seq:D)

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 $cars = ('Ford' => 2'Rover' => 3).BagHash;
say $cars.grab;                                   # OUTPUT: «Ford␤» 
say $cars.grab(2);                                # OUTPUT: «(Rover Rover)␤» 
say $cars.grab(*);                                # OUTPUT: «(Rover Ford)␤» 
 
my $breakfast = ('eggs' => 2'bacon' => 3).Bag;
say $breakfast.grab;
CATCH { default { put .^name''.Str } };
# OUTPUT: «X::Immutable: Cannot call 'grab' on an immutable 'Bag'␤»

(Baggy) method grabpairs §

Defined as:

multi method grabpairs(Baggy:D: --> Any)
multi method grabpairs(Baggy:D: $count --> Seq:D)

Returns a Pair or a Seq of Pairs 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 Pairs 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 $breakfast = (eggs => 2bacon => 3).BagHash;
say $breakfast.grabpairs;                         # OUTPUT: «bacon => 3␤» 
say $breakfast;                                   # OUTPUT: «BagHash.new(eggs(2))␤» 
say $breakfast.grabpairs(1);                      # OUTPUT: «(eggs => 2)␤» 
say $breakfast.grabpairs(*);                      # OUTPUT: «()␤» 
 
my $diet = ('eggs' => 2'bacon' => 3).Bag;
say $diet.grabpairs;
CATCH { default { put .^name''.Str } };
# OUTPUT: «X::Immutable: Cannot call 'grabpairs' on an immutable 'Bag'␤»

(Baggy) method pick §

Defined as:

multi method pick(Baggy:D: --> Any)
multi method pick(Baggy:D: $count --> Seq:D)

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 $breakfast = bag <eggs bacon bacon bacon>;
say $breakfast.pick;                              # OUTPUT: «eggs␤» 
say $breakfast.pick(2);                           # OUTPUT: «(eggs bacon)␤» 
 
say $breakfast.total;                             # OUTPUT: «4␤» 
say $breakfast.pick(*);                           # OUTPUT: «(bacon bacon bacon eggs)␤»

(Baggy) method pickpairs §

Defined as:

multi method pickpairs(Baggy:D: --> Pair:D)
multi method pickpairs(Baggy:D: $count --> Seq:D)

Returns a Pair or a Seq of Pairs 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 Pairs 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 $breakfast = bag <eggs bacon bacon bacon>;
say $breakfast.pickpairs;                         # OUTPUT: «eggs => 1␤» 
say $breakfast.pickpairs(1);                      # OUTPUT: «(bacon => 3)␤» 
say $breakfast.pickpairs(*);                      # OUTPUT: «(eggs => 1 bacon => 3)␤»

(Baggy) method roll §

Defined as:

multi method roll(Baggy:D: --> Any:D)
multi method roll(Baggy:D: $count --> Seq:D)

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 $breakfast = bag <eggs bacon bacon bacon>;
say $breakfast.roll;                                  # OUTPUT: «bacon␤» 
say $breakfast.roll(3);                               # OUTPUT: «(bacon eggs bacon)␤» 
 
my $random_dishes := $breakfast.roll(*);
say $random_dishes[^5];                               # OUTPUT: «(bacon eggs bacon bacon bacon)␤»

(Baggy) method pairs §

Defined as:

method pairs(Baggy:D: --> Seq:D)

Returns all elements and their respective weights as a Seq of Pairs where the key is the element itself and the value is the weight of that element.

my $breakfast = bag <bacon eggs bacon>;
my $seq = $breakfast.pairs;
say $seq.sort;                                    # OUTPUT: «(bacon => 2 eggs => 1)␤»

(Baggy) method antipairs §

Defined as:

method antipairs(Baggy:D: --> Seq:D)

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 $breakfast = bag <bacon eggs bacon>;
my $seq = $breakfast.antipairs;
say $seq.sort;                                    # OUTPUT: «(1 => eggs 2 => bacon)␤»

(Baggy) method invert §

Defined as:

method invert(Baggy:D: --> Seq:D)

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 $breakfast = bag <bacon eggs bacon>;
my $seq = $breakfast.invert;
say $seq.sort;                                    # OUTPUT: «(1 => eggs 2 => bacon)␤»

(Baggy) method classify-list §

Defined as:

multi method classify-list(&mapper*@list --> Baggy:D)
multi method classify-list(%mapper*@list --> Baggy:D)
multi method classify-list(@mapper*@list --> Baggy:D)

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: { $_ %% 2 ?? 'even' !! 'odd' }^10;
# OUTPUT: BagHash(even(5) odd(5)) 
 
my @mapper = <zero one two three four five>;
say MixHash.new.classify-list: @mapper123446;
# 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.

(Baggy) method categorize-list §

Defined as:

multi method categorize-list(&mapper*@list --> Baggy:D)
multi method categorize-list(%mapper*@list --> Baggy:D)
multi method categorize-list(@mapper*@list --> Baggy:D)

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: {
    gather {
        take 'largish' if $_ > 5;
        take .is-prime ?? 'prime' !! 'non-prime';
        take $_ %% 2   ?? 'even'  !! 'odd';
    }
}^10;
# OUTPUT: BagHash(largish(4) even(5) non-prime(6) prime(4) odd(5)) 
 
my %mapper = :sugar<sweet white>:lemon<sour>:cake('sweet''is-a-lie');
say MixHash.new.categorize-list: %mapper, <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.

(Baggy) method keys §

Defined as:

method keys(Baggy:D: --> Seq:D)

Returns a Seq of all keys in the Baggy object without taking their individual weights into account as opposed to kxxv.

my $breakfast = bag <eggs spam spam spam>;
say $breakfast.keys.sort;                        # OUTPUT: «(eggs spam)␤» 
 
my $n = ("a" => 5"b" => 2).BagHash;
say $n.keys.sort;                                # OUTPUT: «(a b)␤»

(Baggy) method values §

Defined as:

method values(Baggy:D: --> Seq:D)

Returns a Seq of all values, i.e. weights, in the Baggy object.

my $breakfast = bag <eggs spam spam spam>;
say $breakfast.values.sort;                      # OUTPUT: «(1 3)␤» 
 
my $n = ("a" => 5"b" => 2"a" => 1).BagHash;
say $n.values.sort;                              # OUTPUT: «(2 6)␤»

(Baggy) method kv §

Defined as:

method kv(Baggy:D: --> Seq:D)

Returns a Seq of keys and values interleaved.

my $breakfast = bag <eggs spam spam spam>;
say $breakfast.kv;                                # OUTPUT: «(spam 3 eggs 1)␤» 
 
my $n = ("a" => 5"b" => 2"a" => 1).BagHash;
say $n.kv;                                        # OUTPUT: «(a 6 b 2)␤»

(Baggy) method kxxv §

Defined as:

method kxxv(Baggy:D: --> Seq:D)

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 $breakfast = bag <spam eggs spam spam bacon>;
say $breakfast.kxxv.sort;                         # OUTPUT: «(bacon eggs spam spam spam)␤» 
 
my $n = ("a" => 0"b" => 1"b" => 2).BagHash;
say $n.kxxv;                                      # OUTPUT: «(b b b)␤»

(Baggy) method elems §

Defined as:

method elems(Baggy:D: --> Int:D)

Returns the number of elements in the Baggy object without taking the individual elements' weight into account.

my $breakfast = bag <eggs spam spam spam>;
say $breakfast.elems;                             # OUTPUT: «2␤» 
 
my $n = ("b" => 9.4"b" => 2).MixHash;
say $n.elems;                                     # OUTPUT: «1␤»

(Baggy) method total §

Defined as:

method total(Baggy:D:)

Returns the sum of weights for all elements in the Baggy object.

my $breakfast = bag <eggs spam spam bacon>;
say $breakfast.total;                             # OUTPUT: «4␤» 
 
my $n = ("a" => 5"b" => 1"b" => 2).BagHash;
say $n.total;                                     # OUTPUT: «8␤»

(Baggy) method default §

Defined as:

method default(Baggy:D: --> 0)

Returns zero.

my $breakfast = bag <eggs bacon>;
say $breakfast.default;                           # OUTPUT: «0␤»

(Baggy) method hash §

Defined as:

method hash(Baggy:D: --> Hash:D)

Returns a Hash where the elements of the invocant are the keys and their respective weights the values.

my $breakfast = bag <eggs bacon bacon>;
my $h = $breakfast.hash;
say $h.^name;                    # OUTPUT: «Hash[Any,Any]␤» 
say $h;                          # OUTPUT: «{bacon => 2, eggs => 1}␤»

(Baggy) method Bool §

Defined as:

method Bool(Baggy:D: --> Bool:D)

Returns True if the invocant contains at least one element.

my $breakfast = ('eggs' => 1).BagHash;
say $breakfast.Bool;                              # OUTPUT: «True␤» 
                                                  # (since we have one element) 
$breakfast<eggs> = 0;                             # weight == 0 will lead to element removal 
say $breakfast.Bool;                              # OUTPUT: «False␤»

(Baggy) method Set §

Defined as:

method Set(--> Set:D)

Returns a Set whose elements are the keys of the invocant.

my $breakfast = (eggs => 2bacon => 3).BagHash;
say $breakfast.Set;                               # OUTPUT: «Set(bacon eggs)␤»

(Baggy) method SetHash §

Defined as:

method SetHash(--> SetHash:D)

Returns a SetHash whose elements are the keys of the invocant.

my $breakfast = (eggs => 2bacon => 3).BagHash;
my $sh = $breakfast.SetHash;
say $sh.^name;                            # OUTPUT: «SetHash␤» 
say $sh.elems;                            # OUTPUT: «2␤»

(Baggy) method ACCEPTS §

Defined as:

method ACCEPTS($other --> Bool:D)

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 $breakfast = bag <eggs bacon>;
say $breakfast ~~ Baggy;                            # OUTPUT: «True␤» 
say $breakfast.does(Baggy);                         # OUTPUT: «True␤» 
 
my $second-breakfast = (eggs => 1bacon => 1).Mix;
say $breakfast ~~ $second-breakfast;                # OUTPUT: «True␤» 
 
my $third-breakfast = (eggs => 1bacon => 2).Bag;
say $second-breakfast ~~ $third-breakfast;          # OUTPUT: «False␤»

Routines supplied by role QuantHash §

BagHash 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:D:)

Returns a list of Pair objects of all keys and values in the QuantHash.

(QuantHash) method Setty §

method Setty(--> Setty:D)

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 %b is Bag = one => 1two => 2;
say %b.Setty# OUTPUT: «Set(one two)␤» 
my %m is Mix = one => 1minus => -1;
say %m.Setty# OUTPUT: «Set(one)␤»

(QuantHash) method Baggy §

method Baggy(--> Baggy:D)

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 %s is Set = <one two>;
say %s.Baggy# OUTPUT: «Bag(one two)␤» 
my %m is Mix = one => 1minus => -1;
say %m.Baggy# OUTPUT: «Bag(one)␤»

(QuantHash) method Mixy §

method Mixy(--> Mixy:D)

Coerce the QuantHash object to the equivalent object that uses the Mixy role.

my %s is Set = <one two>;
say %s.Mixy# OUTPUT: «Mix(one two)␤» 
my %b is Bag = one => 1two => 2;
say %b.Mixy# OUTPUT: «Mix(one two(2))␤»

Routines supplied by role Associative §

BagHash 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 %any-hash;
say %any-hash.of# OUTPUT: «(Mu)␤»

The value is the first parameter you use when instantiating Associative with particular classes:

class DateHash is Hash does Associative[Cool,DateTime{};
my %date-hash := DateHash.new;
say %date-hash.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 %any-hash;
%any-hash.keyof# OUTPUT: «(Str(Any))␤»

(Associative) method AT-KEY §

method AT-KEY(\key)

Should return the value / container at the given key.

class What { method AT-KEY(\key{ 42 }};
say What.new{33}# 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:$INITIALIZE)

This method should only be supplied if you want to support the:

my %h is Foo = => 42=> 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 Pairs, 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.

Generated from

Generated from Type/BagHash.pod6