@@ -20,22 +20,53 @@ say $fruits.elems; # OUTPUT: «3»
20
20
say $fruits.keys.sort; # OUTPUT: «apple orange peach»
21
21
= end code
22
22
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.
27
26
28
27
= begin code
29
28
my $fruits = <peach apple orange apple apple>.SetHash;
30
29
31
30
say $fruits<apple>; # OUTPUT: «True»
32
31
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;
33
63
34
64
$fruits<apple kiwi> = False, True;
35
65
say $fruits.keys.sort; # OUTPUT: «kiwi orange peach»
36
66
= end code
37
67
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:
39
70
40
71
= begin code
41
72
my SetHash $fruits .= new;
@@ -133,6 +164,35 @@ say $a ⊖ $b; # OUTPUT: «SetHash(1 3 4)»
133
164
say $a ∪ $b; # OUTPUT: «SetHash(1 2 3 4)»
134
165
= end code
135
166
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
+
136
196
= head1 See Also
137
197
138
198
L < Sets, Bags, and Mixes|/language/setbagmix >
0 commit comments