Skip to content

Commit 2803bc3

Browse files
committed
Add the add and remove BagHash methods
This commit adds method documentation for the `add` and `remove` methods on the `BagHash` class and adds narrative documentation and examples showing their use. It also adds "baghash's" to the spellcheck dictionary to prevent a test failure.
1 parent ae9fec8 commit 2803bc3

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

doc/Type/BagHash.pod6

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ or the
3333
L«C< < > > postcircumfix operator|/language/operators#postcircumfix_<_>»
3434
for literal string keys, which
3535
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).
4039
4140
=begin code
4241
my $breakfast = <spam eggs spam spam bacon spam>.BagHash;
@@ -103,6 +102,40 @@ or using the masquerading syntax:
103102
my %bh is BagHash[Int] = <a b b c c c>;
104103
# Type check failed in binding; expected Int but got Str ("a")
105104
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+
106139
=head1 Operators
107140
108141
See L<Operators with set
@@ -143,6 +176,32 @@ say $a.sort; # OUTPUT: «(2 => 2 3 => 1 4 => 1 18 => 1)␤»
143176
say $a.sort.reverse; # OUTPUT: «(18 => 1 4 => 1 3 => 1 2 => 2)␤»
144177
=end code
145178
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.
146205
147206
=head1 See Also
148207

xt/pws/words.pws

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ backtrace
125125
backtraces
126126
baggy's
127127
baghash
128+
baghash's
128129
baghashes
129130
baldr
130131
barewords

0 commit comments

Comments
 (0)