role Setty

Collection of distinct objects

role Setty does QuantHash { }

A role for collections which make sure that each element can only appear once. See Set and SetHash.

Methods

method grab

method grab($count = 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.

method grabpairs

method grabpairs($count = 1)

Removes $count elements chosen at random (without repetition) from the set, and returns a list of Pair objects whose keys are the grabbed elements and whose values are True.

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 as Pairs in the aforementioned way in random order.

Only works on mutable sets; When used on an immutable set, it results in an exception.

method pick

multi method pick($count = 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.

method roll

multi method roll($count = 1)

Returns a lazy list of $count elements, each randomly selected from the set. Each random choice is made independently, like a separate die roll where each die face is a set element.

If * is passed as $count, the list is infinite.

method keys

Defined as:

multi method keys(Setty:D:) returns Seq:D

Returns a Seq of all elements of the set.

my $s = Set.new(1, 2, 3);
say $s.keys;                                      # (3 1 2)

method values

Defined as:

multi method values(Setty:D:) returns Seq:D

Returns a Seq containing as many True values as the set has elements.

my $s = Set.new(1, 2, 3);
say $s.values;                                    # (True True True)

method kv

Defined as:

multi method kv(Setty:D:) returns Seq:D

Returns a Seq of the set's elements and True values interleaved.

my $s = Set.new(1, 2, 3);
say $s.kv;                                        # (3 True 1 True 2 True)

method elems

method elems(--> Int)

The number of elements of the set.

method total

method total(--> Int)

The total of all the values of the QuantHash object. For a Setty object, this is just the number of elements.

method default

Defined as:

method default returns False

Returns the default value of the invocant, i.e. the value which is returned when trying to access an element in the Setty object which has not been previously initialized or when accessing an element which has explicitly been set to Nil or False.

my $s1 = SetHash.new(1, 2, 3);
say $s1{2};                                           # True
$s1{2} = Nil;
say $s1{2};                                           # False
# access non initialized element
say $s1{4};                                           # False

method ACCEPTS

method ACCEPTS($other)

Returns True if $other and self contain all the same elements, and no others.

method Bag

Defined as:

method Bag(Setty:D:) returns Bag:D

Returns a Bag containing the elements of the invocant.

my Bag $b = Set.new(1, 2, 3).Bag;
say $b;                                           # bag(3, 1, 2)

method BagHash

Defined as:

method BagHash(Setty:D:) returns BagHash:D

Returns a BagHash containing the elements of the invocant.

my BagHash $b = Set.new(1, 2, 3).BagHash;
say $b;                                           # BagHash.new(3, 1, 2)

method Bool

Defined as:

multi method Bool(Setty:D:) returns Bool:D

Returns True if the invocant contains at least one element.

my $s1 = Set.new(1, 2, 3);
say $s1.Bool;                                     # True

my $s2 = $s1Set.new(4, 5);                     # set intersection operator
say $s2.Bool;                                     # False

See Also

Sets, Bags, and Mixes

Type graph

Type relations for Setty
perl6-type-graph Setty Setty QuantHash QuantHash Setty->QuantHash Associative Associative QuantHash->Associative Mu Mu Any Any Any->Mu Set Set Set->Setty Set->Any SetHash SetHash SetHash->Setty SetHash->Any

Stand-alone image: vector, raster