@@ -33,10 +33,9 @@ or the
33
33
L « C < < > > postcircumfix operator|/language/operators#postcircumfix_<_>»
34
34
for literal string keys, which
35
35
returns the corresponding integer weight for keys that are
36
- elements of the bag, and C < 0 > for keys that aren't. They can also be used to
37
- modify weights; setting a weight to C < 0 > automatically removes that element from
38
- the bag, and setting a weight to a positive number adds that element if it
39
- didn't already exist:
36
+
37
+ elements of the bag, and C < 0 > for keys that aren't. These operators can also be
38
+ used to modify weights (see L < Updating BagHash Objects|#Updating_BagHash_Objects > , below).
40
39
41
40
= begin code
42
41
my $breakfast = <spam eggs spam spam bacon spam>.BagHash;
@@ -103,6 +102,40 @@ or using the masquerading syntax:
103
102
my %bh is BagHash[Int] = <a b b c c c>;
104
103
# Type check failed in binding; expected Int but got Str ("a")
105
104
105
+ = head1 Updating BagHash Objects
106
+
107
+ Once you have created a C < BagHash > , you can update its values in two
108
+ ways. First, you can use the C < add > and C < remove > methods:
109
+
110
+ my $n = BagHash.new: "a", "b", "c", "c";
111
+ say $n.raku; # OUTPUT: «("b"=>1,"a"=>1,"c"=>2).BagHash»
112
+ $n.add('c');
113
+ say $n.raku; # OUTPUT: «("b"=>1,"c"=>3,"a"=>1).BagHash»
114
+ $n.remove(('b', 'a'),);
115
+ say $n.raku; # OUTPUT: «("c"=>3).BagHash»
116
+ $n.remove('c');
117
+ say $n.raku; # OUTPUT: «("c"=>2).BagHash»
118
+
119
+ Note that, as shown in the final example, the C < remove > method removes
120
+ a I < single > value from the C < BagHash > ; it doesn't entirely remove the
121
+ key from the C < BagHash > .
122
+
123
+ Alternatively, you can use assignment (including with L < autoincrement
124
+ operators|/language/operators#Autoincrement_precedence > such as C < ++ >
125
+ and C < -- > ) to modify the C < BagHash > 's contents.
126
+
127
+ my $n = BagHash.new: "a", "b", "c", "c";
128
+ say $n.raku; # OUTPUT: «("b"=>1,"a"=>1,"c"=>2).BagHash»
129
+ $n<c>++;
130
+ say $n.raku; # OUTPUT: «("b"=>1,"c"=>3,"a"=>1).BagHash»
131
+ $n<b> -= 1;
132
+ say $n.raku; # OUTPUT: «("a"=>1,"c"=>3).BagHash»
133
+ $n{'a'} = 0;
134
+ say $n.raku; # OUTPUT: «("c"=>3).BagHash»
135
+
136
+ Using either syntax, if you set the value of an item to zero or less
137
+ than zero, the item will be removed from the C < BagHash > .
138
+
106
139
= head1 Operators
107
140
108
141
See L < Operators with set
@@ -143,6 +176,32 @@ say $a.sort; # OUTPUT: «(2 => 2 3 => 1 4 => 1 18 => 1)»
143
176
say $a.sort.reverse; # OUTPUT: «(18 => 1 4 => 1 3 => 1 2 => 2)»
144
177
= end code
145
178
179
+ = head2 method add
180
+
181
+ method add(BagHash: \to-add, *%_ --> Nil)
182
+
183
+ When given single item, C < add > adds it to the C < BagHash > or, if it was
184
+ already present, increases its weight by 1. When given a C < List > ,
185
+ C < Array > , C < Seq > , or any other type that C < does > the
186
+ L < C « Iterator » |/type/Iterator> Role, C < add > adds each element of the
187
+ C < Iterator > to the C < SetHash > or increments the weight of each element
188
+ by 1.
189
+
190
+ I < Note: > since version 2020.02.
191
+
192
+ = head2 method remove
193
+
194
+ method remove(BagHash: \to-remove, *%_ --> Nil)
195
+
196
+ When given single item, C < remove > reduces the weight of that item by
197
+ one. If this results in the item having a weight of 0, this removes
198
+ the item from the C < BagHash > . If the item is not present in the
199
+ C < BagHash > , C < remove > has no effect. When given a C < List > , C < Array > ,
200
+ C < Seq > , or any other type that C < does > the
201
+ L < C « Iterator » |/type/Iterator> Role, C < remove > reduces the weight of
202
+ each element by 1 and removes any items with the resulting weight of 0.
203
+
204
+ I < Note: > since version 2020.02.
146
205
147
206
= head1 See Also
148
207
0 commit comments