Rules / Types, classes & roles / composite

Set Reference

Immutable collection of distinct objects

What it is #

Routines #

Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.

sub set #

sub set(*@args --> Set)

Returns Set.

Creates a Set from the given @args Sets, Bags, and Mixes

Examples, run three ways #

Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.

1 doc-drift · 5 no-output · 5 ok · 1 rakudo-differs · 1 rakupp-differs

class Set does Setty { }

Not executed: the documentation states no expected output for this example.

my $fruits = set <peach apple orange apple apple>;

say $fruits.elems;      # OUTPUT: «3␤»
say $fruits.keys.sort;  # OUTPUT: «apple orange peach␤»
Documentation
3
apple orange peach
Rakudo
3
(apple orange peach)
Raku++
3
(apple orange peach)

Both engines agree; the documentation states something else. Trust the engines.

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

Documentation, Rakudo and Raku++ all agree.

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

Raku++ disagrees with both the documentation and Rakudo — a defect.

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

Raku++ matches the documentation; Rakudo does not. This is the one class where neither engine can be assumed right: it may be a stale doc that Raku++ was built from, or it may be a Rakudo bug that the documentation predates. Each case is examined individually.

Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.

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

Documentation, Rakudo and Raku++ all agree.

my $fruits = Set.new( <peach apple orange apple apple> );

Not executed: the documentation states no expected output for this example.

# only allow strings (Str) in the Set
my $fruits = Set[Str].new( <peach apple orange apple apple> );

Not executed: the documentation states no expected output for this example.

# only allow whole numbers (Int) in the Set
my $fruits = Set[Int].new( <peach apple orange apple apple> );
# Type check failed in binding; expected Int but got Str ("peach")

Not executed: the documentation states no expected output for this example.

my %s is Set = <a b c>;
say %s<a>;  # OUTPUT: «True␤»
say %s<d>;  # OUTPUT: «False␤»
Output
True
False

Documentation, Rakudo and Raku++ all agree.

# limit to strings
my %s is Set[Str] = <a b c>;
say %s<a>;  # OUTPUT: «True␤»
say %s<d>;  # OUTPUT: «False␤»
Output
True
False

Documentation, Rakudo and Raku++ all agree.

# limit to whole numbers
my %s is Set[Int] = <a b c>;
# Type check failed in binding; expected Int but got Str ("a")

Not executed: the documentation states no expected output for this example.

my ($a, $b) = set(1, 2, 3), set(2, 4);

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)␤»
Output
False
Set(2)
Set(1 3 4)
False
Set(2)
Set(1 3 4)

Documentation, Rakudo and Raku++ all agree.