role QuantHash

Collection of objects represented as hash keys

role QuantHash does Associative { }

A QuantHash represents a set of objects, represented as the keys of a Hash. When asked to behave as a list it ignores its .values and returns only .keys (possibly replicated by weight in the case of bag types). For any QuantHash, the .total methods returns the current sum of the values.

All standard QuantHash containers have a default value that is false (such as 0 or '' or Nil or Bool::False), and keep around only those entries with non-default values, automatically deleting any entry if its value goes to that (false) default value.

Type Graph

Type relations for QuantHash
perl6-type-graph QuantHash QuantHash Associative Associative QuantHash->Associative Setty Setty Setty->QuantHash Baggy Baggy Baggy->QuantHash Mu Mu Any Any Any->Mu Set Set Set->Setty Set->Any SetHash SetHash SetHash->Setty SetHash->Any Bag Bag Bag->Baggy Bag->Any BagHash BagHash BagHash->Baggy BagHash->Any Mixy Mixy Mixy->Baggy Mix Mix Mix->Any Mix->Mixy MixHash MixHash MixHash->Any MixHash->Mixy

Expand above chart

Routines supplied by role Associative

QuantHash does role Associative, which provides the following routines:

(Associative) method of

Defined as:

method of()

Associative is actually a parameterized role which can use different classes for keys and values. As seen above, by default it coerces to Str for the key 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 :=;
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.

(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.