Rules / Types, classes & roles / composite

SetHash Reference

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

method set #

method set(SetHash:D: \to-set --> Nil)

Returns Nil.

When given single key, set adds it to the SetHash. When given a List, Array, Seq, or any other type that does the Iterator Role, set adds each element of the Iterator as a key to the SetHash. Note: since release 2020.02.

method unset #

method unset(SetHash:D: \to-unset --> Nil)

Returns Nil.

When given single key, unset removes it from the SetHash. When given a List, Array, Seq, or any other type that does the Iterator Role, unset removes each element of the Iterator from the SetHash (if it was present as a key). Note: since release 2020.02. 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 all-differ · 2 doc-drift · 3 no-output · 6 ok · 2 rakupp-differs

class SetHash does Setty { }

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

my $fruits = <peach apple orange apple apple>.SetHash;

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 = <peach apple orange apple apple>.SetHash;

say $fruits<apple>;     # OUTPUT: «True␤»
say $fruits<kiwi>;      # OUTPUT: «False␤»
Output
True
False

Documentation, Rakudo and Raku++ all agree.

my $fruits = <peach>.SetHash;
$fruits.set('apple');
say $fruits;            # OUTPUT: «SetHash(apple peach)␤»

$fruits.unset('peach');
say $fruits;            # OUTPUT: «SetHash(apple)␤»

$fruits.set(<kiwi banana apple>);
say $fruits;            # OUTPUT: «SetHash(apple banana kiwi)␤»

$fruits.unset(<apple banana kiwi>);
say $fruits;            # OUTPUT: «SetHash()␤»
Output
SetHash(apple peach)
SetHash(apple)
SetHash(apple banana kiwi)
SetHash()

Documentation, Rakudo and Raku++ all agree.

my $fruits = <peach apple orange>.SetHash;

$fruits<apple kiwi> = False, True;
say $fruits.keys.sort;  # OUTPUT: «kiwi orange peach␤»
Documentation
kiwi orange peach
Rakudo
(kiwi orange peach)
Raku++
(kiwi orange peach)

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

my SetHash $fruits .= new;
say $fruits<cherry>;      # OUTPUT: «False␤»
$fruits<cherry>++;
say $fruits<cherry>;      # OUTPUT: «True␤»
$fruits<apple banana kiwi>»++; # Add multiple elements

$fruits<cherry>--;
say $fruits<cherry>;      # OUTPUT: «False␤»
$fruits<banana kiwi>»--; # Remove multiple elements
Documentation
False
True
False
Rakudo
False
True
False
Raku++
False
1
False

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

my $n = SetHash.new: "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
(:one(1), :zero(0), :two(2)).Seq
((Pair) (Pair) (Pair))
Raku++
(:one(1), :two(2), :zero(0)).Seq
((Pair) (Pair) (Pair))

All three differ. Needs a human.

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

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

Documentation, Rakudo and Raku++ all agree.

my $sh = SetHash.new;
$sh{ 'key1' } = True;
$sh{ 'key2' } = 'Hello World!';
$sh{ 'key3' } = 0;  # does not store the key since 0.Bool is False
say $sh;            # OUTPUT: «SetHash(key1 key2)␤»
say $sh.keys.raku;  # OUTPUT: «("key1", "key2").Seq␤»
Output
SetHash(key1 key2)
("key1", "key2").Seq

Documentation, Rakudo and Raku++ all agree.

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

Documentation, Rakudo and Raku++ all agree.

# only allow Pairs
my $n = SetHash[Pair].new: "zero" => 0, "one" => 1, "two" => 2;

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

# only allow strings
my %sh is SetHash[Str] = <a b c>;
say %sh<a>;  # OUTPUT: «True␤»
say %sh<d>;  # OUTPUT: «False␤»
Output
True
False

Documentation, Rakudo and Raku++ all agree.

# only allow whole numbers
my %sh is SetHash[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) = SetHash.new(1, 2, 3), SetHash.new(2, 4);

say $a (<) $b;  # OUTPUT: «False␤»
say $a (&) $b;  # OUTPUT: «SetHash(2)␤»
say $a (^) $b;  # OUTPUT: «SetHash(1 3 4)␤»
say $a (|) $b;  # OUTPUT: «SetHash(1 2 3 4)␤»

# Unicode versions:
say $a ⊂ $b;  # OUTPUT: «False␤»
say $a ∩ $b;  # OUTPUT: «SetHash(2)␤»
say $a ⊖ $b;  # OUTPUT: «SetHash(1 3 4)␤»
say $a ∪ $b;  # OUTPUT: «SetHash(1 2 3 4)␤»
Documentation
False
SetHash(2)
SetHash(1 3 4)
SetHash(1 2 3 4)
False
SetHash(2)
SetHash(1 3 4)
SetHash(1 2 3 4)
Rakudo
False
SetHash(2)
SetHash(1 3 4)
SetHash(1 2 3 4)
False
SetHash(2)
SetHash(1 3 4)
SetHash(1 2 3 4)
Raku++
False
Set(2)
Set(1 3 4)
Set(1 2 3 4)
False
Set(2)
Set(1 3 4)
Set(1 2 3 4)

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