Skip to content

Commit d29b507

Browse files
authored
Document the set and unset SetHash methods (#3583)
Add method-level documentation and overview examples using the set and unset methods on HashSet.
1 parent 88870c7 commit d29b507

File tree

1 file changed

+65
-5
lines changed

1 file changed

+65
-5
lines changed

doc/Type/SetHash.pod6

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,53 @@ say $fruits.elems; # OUTPUT: «3␤»
2020
say $fruits.keys.sort; # OUTPUT: «apple orange peach␤»
2121
=end code
2222
23-
C<SetHash>es can be treated as object hashes using the C<{ }> postcircumfix
24-
operator, which returns the value C<True> for keys that are elements of the set,
25-
and C<False> for keys that aren't. Assigning a value that boolifies to C<True>
26-
or C<False>, respectively, can be used to add or remove a set element:
23+
Just like C<Set>s, C<SetHash>es can be treated as object hashes using the C<{ }>
24+
postcircumfix operator, which returns the value C<True> for keys that are
25+
elements of the set, and C<False> for keys that aren't.
2726
2827
=begin code
2928
my $fruits = <peach apple orange apple apple>.SetHash;
3029
3130
say $fruits<apple>; # OUTPUT: «True␤»
3231
say $fruits<kiwi>; # OUTPUT: «False␤»
32+
=end code
33+
34+
Unlike C<Set>s, C<SetHash>es are mutable. You can add an item or list of items
35+
to the C<SetHash> with the C<set> method and can remove an item or list of items
36+
with the C<unset> method:
37+
38+
=begin code
39+
my $fruits = <peach>.SetHash;
40+
$fruits.set('apple');
41+
say $fruits; # OUTPUT: «SetHash(apple peach)␤»
42+
43+
$fruits.unset('peach');
44+
say $fruits; # OUTPUT: «SetHash(apple)␤»
45+
46+
$fruits.set(<kiwi banana apple>);
47+
say $fruits; # OUTPUT: «SetHash(apple banana kiwi)␤»
48+
49+
$fruits.unset(<apple banana kiwi>);
50+
say $fruits; # OUTPUT: «SetHash()␤»
51+
=end code
52+
53+
Be careful not to confuse the C<set> method, which adds an item to a C<SetHash>
54+
with the C<Set> method, which converts the mutable C<SetHash> into an immutable
55+
C<Set>.
56+
57+
As an alternative to using the C<set> and C<unset> methods, you can also add or
58+
remove set elements by assigning a value that boolifies to C<True> or C<False>,
59+
respectively:
60+
61+
=begin code
62+
my $fruits = <peach apple orange>.SetHash;
3363
3464
$fruits<apple kiwi> = False, True;
3565
say $fruits.keys.sort; # OUTPUT: «kiwi orange peach␤»
3666
=end code
3767
38-
Here is a convenient shorthand idiom for adding and removing SetHash elements:
68+
Here is a convenient shorthand idiom for adding and removing SetHash elements
69+
using assignment:
3970
4071
=begin code
4172
my SetHash $fruits .= new;
@@ -133,6 +164,35 @@ say $a ⊖ $b; # OUTPUT: «SetHash(1 3 4)␤»
133164
say $a ∪ $b; # OUTPUT: «SetHash(1 2 3 4)␤»
134165
=end code
135166
167+
=head1 Methods
168+
169+
=head2 method set
170+
171+
Defined as:
172+
173+
method set(SetHash:D: \to-set --> Nil)
174+
175+
When given single key, C<set> adds it to the C<SetHash>. When given a
176+
C<List>, C<Array>, C<Seq>, or any other type that C<does> the
177+
L<C«Iterator»|/type/Iterator> Role, C<set> adds each element of the
178+
C<Iterator> as key to the C<SetHash>.
179+
180+
I<Note:> since version 2020.02.
181+
182+
=head2 method unset
183+
184+
Defined as:
185+
186+
method unset(SetHash:D: \to-unset --> Nil)
187+
188+
When given single key, C<set> removes it from the C<SetHash>. When
189+
given a C<List>, C<Array>, C<Seq>, or any other type that C<does> the
190+
L<C«Iterator»|/type/Iterator> Role, C<set> removes each element of the
191+
C<Iterator> from the C<SetHash> (if it was present as a key).
192+
193+
I<Note:> since version 2020.02.
194+
195+
136196
=head1 See Also
137197
138198
L<Sets, Bags, and Mixes|/language/setbagmix>

0 commit comments

Comments
 (0)