class Set

Immutable collection of distinct objects

class Set does Setty { }

A Set is an immutable set, meaning a collection of distinct elements in no particular order. (For mutable sets, see SetHash instead.)

Objects/values of any type are allowed as set elements. Within a Set, every element is guaranteed to be unique (in the sense that no two elements would compare positively with the === operator):

my $fruits = set <peach apple orange apple apple>;
 
say $fruits.elems;      # OUTPUT: «3␤» 
say $fruits.keys.sort;  # OUTPUT: «apple orange peach␤» 

Sets can be treated as object hashes using the { } postcircumfix operator, which returns the value True for keys that are elements of the set, and False for keys that aren't:

my $fruits = set <peach apple orange apple apple>;
say $fruits<apple>;  # OUTPUT: «True␤» 
say $fruits<kiwi>;   # OUTPUT: «False␤» 

Creating Set objects

Sets can be composed using the set subroutine (or Set.new, for which it is a shorthand). Any positional parameters, regardless of their type, become elements of the set:

my $n = set "zero" => 0"one" => 1"two" => 2;
say $n.keys.perl;        # OUTPUT: «(:two(2), :zero(0), :one(1)).Seq␤» 
say $n.keys.map(&WHAT);  # OUTPUT: «((Pair) (Pair) (Pair))␤» 

Alternatively, the .Set coercer (or its functional form, Set()) can be called on an existing object to coerce it to a Set. Its semantics depend on the type and contents of the object. In general it evaluates the object in list context and creates a set with the resulting items as elements, although for Hash-like objects or Pair items, only the keys become elements of the set - and keys mapped to values which boolify to False are skipped:

my $n = ("zero" => 0"one" => 1"two" => 2).Set;
say $n.keys.perl;        # OUTPUT: «("one", "two").Seq␤» 
say $n.keys.map(&WHAT);  # OUTPUT: «((Str) (Str))␤» 

Furthermore, you can get a Set by using set operators (see next section) on objects of other types such as List, which will internally call .Set on them before performing the operation. Be aware of the tight precedence of those operators though, which may require you to use parentheses around arguments:

say (1..5(^) 4;  # OUTPUT: «set(1, 2, 3, 5)␤» 

Operators

Perl 6 provides common set operators, which can take Sets (or any other collections) as input, and return result sets as new Set objects. For example:

my ($a$b= set(123), set(24);
 
say $a (<) $b;  # OUTPUT: «False␤» 
say $a (&) $b;  # OUTPUT: «set(2)␤» 
say $a (^) $b;  # OUTPUT: «set(1, 3, 4)␤» 
 
# Unicode versions: 
say $a  $b;  # OUTPUT: «False␤» 
say $a  $b;  # OUTPUT: «set(2)␤» 
say $a  $b;  # OUTPUT: «set(1, 3, 4)␤» 

See Set/Bag Operators for a complete list of set operators with detailed explanations.

Subroutines

sub set

sub set(*@args --> Set)

Creates a Set from the given @args

See Also

Sets, Bags, and Mixes

Type Graph

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

Stand-alone image: vector

Routines supplied by role Setty

Set does role Setty, which provides the following routines:

(Setty) method new-from-pairs

Defined as:

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

Constructs a Setty object from a list of Pair objects given as positional arguments:

say Set.new-from-pairs: 'butter' => 0.22'salt' => 0'sugar' => 0.02;
# OUTPUT: «set(butter, sugar)␤» 

Note: be sure you aren't accidentally passing the Pairs as positional arguments; the quotes around the keys in the above example are significant.

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

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

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

(Setty) method pickpairs

Defined as:

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

Returns a Pair or a Seq of Pairs depending on the candidate of the method being invoked. Each Pair returned has an element of the invocant as its key and True as its value. In contrast to grabpairs, 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/True 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 $numbers = set (423);
say $numbers.pickpairs;                           # OUTPUT: «4 => True␤» 
say $numbers.pickpairs(1);                        # OUTPUT: «(3 => True)␤» 
say $numbers.pickpairs(*);                        # OUTPUT: «(2 => True 4 => True 3 => True)␤» 

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

(Setty) method keys

Defined as:

multi method keys(Setty:D: --> Seq:D)

Returns a Seq of all elements of the set.

my $s = Set.new(123);
say $s.keys;                                      # OUTPUT: «(3 1 2)␤» 

(Setty) method values

Defined as:

multi method values(Setty:D: --> Seq:D)

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

my $s = Set.new(123);
say $s.values;                                    # OUTPUT: «(True True True)␤» 

(Setty) method kv

Defined as:

multi method kv(Setty:D: --> Seq:D)

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

my $s = Set.new(123);
say $s.kv;                                        # OUTPUT: «(3 True 1 True 2 True)␤» 

(Setty) method elems

method elems(--> Int)

The number of elements of the set.

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

(Setty) method minpairs

Defined As:

multi method minpairs(Setty:D: --> Seq:D)

Returns the value of self.pairs (as all Pairs have minimum values). See also Any.minpairs

(Setty) method maxpairs

Defined As:

multi method maxpairs(Setty:D: --> Seq:D)

Returns the value of self.pairs (as all Pairs have maximum values). See also Any.maxpairs

(Setty) method default

Defined as:

method default(--> 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(123);
say $s1{2};                                           # OUTPUT: «True␤» 
$s1{2} = Nil;
say $s1{2};                                           # OUTPUT: «False␤» 
# access non initialized element 
say $s1{4};                                           # OUTPUT: «False␤» 

(Setty) method ACCEPTS

method ACCEPTS($other)

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

(Setty) method Bag

Defined as:

method Bag(Setty:D: --> Bag:D)

Returns a Bag containing the elements of the invocant.

my Bag $b = Set.new(123).Bag;
say $b;                                           # OUTPUT: «bag(3, 1, 2)␤» 

(Setty) method BagHash

Defined as:

method BagHash(Setty:D: --> BagHash:D)

Returns a BagHash containing the elements of the invocant.

my BagHash $b = Set.new(123).BagHash;
say $b;                                           # OUTPUT: «BagHash.new(3, 1, 2)␤» 

(Setty) method Bool

Defined as:

multi method Bool(Setty:D: --> Bool:D)

Returns True if the invocant contains at least one element.

my $s1 = Set.new(123);
say $s1.Bool;                                     # OUTPUT: «True␤» 
 
my $s2 = $s1  Set.new(45);                     # set intersection operator 
say $s2.Bool;                                     # OUTPUT: «False␤» 

(Setty) method Mix

Defined as:

method Mix(Setty:D: --> Mix:D)

Returns a Mix containing the elements of the invocant.

my Mix $b = Set.new(123).Mix;
say $b;                                           # OUTPUT: «mix(3, 1, 2)␤» 

(Setty) method MixHash

Defined as:

method MixHash(Setty:D: --> MixHash:D)

Returns a MixHash containing the elements of the invocant.

my MixHash $b = Set.new(123).MixHash;
say $b;                                           # OUTPUT: «MixHash.new(3, 1, 2)␤»