Skip to content

Commit 0eaa8a8

Browse files
authored
Improve BagHash.new examples(Close #1629)
#1629
1 parent 2c3f82f commit 0eaa8a8

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

doc/Type/BagHash.pod6

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,18 @@ say $breakfast.kxxv.sort; # OUTPUT: «eggs sausage sausage spam spam spam spam
5050
C<BagHash>es can be composed using C<BagHash.new>. Any positional parameters,
5151
regardless of their type, become elements of the bag:
5252
53-
my $n = BagHash.new: "a" => 0, "b" => 1, "c" => 2, "c" => 2;
54-
say $n.keys.perl; # OUTPUT: «(:c(2), :b(1), :a(0)).Seq␤»
55-
say $n.keys.map(&WHAT); # OUTPUT: «((Pair) (Pair) (Pair))␤»
56-
say $n.values.perl; # OUTPUT: «(2, 1, 1).Seq␤»
53+
my $n = BagHash.new: "a", "b", "c", "c";
54+
say $n.perl; # OUTPUT: «("b"=>1,"a"=>1,"c"=>2).BagHash␤»
55+
say $n.keys.perl; # OUTPUT: «("b", "a", "c").Seq␤»
56+
say $n.values.perl; # OUTPUT: «(1, 1, 2).Seq␤»
57+
58+
Besides, C<BagHash.new-from-pairs> can create a C<BagHash> with items and their
59+
occurrences.
60+
61+
my $n = BagHash.new-from-pairs: "a" => 0, "b" => 1, "c" => 2, "c" => 2;
62+
say $n.perl; # OUTPUT: «("b"=>1,"c"=>4).BagHash␤»
63+
say $n.keys.perl; # OUTPUT: «("b", "c").Seq␤»
64+
say $n.values.perl; # OUTPUT: «(1, 4).Seq␤»
5765
5866
Alternatively, the C<.BagHash> coercer (or its functional form, C<BagHash()>)
5967
can be called on an existing object to coerce it to a C<BagHash>. Its semantics
@@ -62,10 +70,10 @@ object in list context and creates a bag with the resulting items as elements,
6270
although for Hash-like objects or Pair items, only the keys become elements of
6371
the bag, and the (cumulative) values become the associated integer weights:
6472
73+
my $m = ("a", "b", "c", "c").BagHash;
6574
my $n = ("a" => 0, "b" => 1, "c" => 2, "c" => 2).BagHash;
66-
say $n.keys.perl; # OUTPUT: «("b", "c").Seq␤»
67-
say $n.keys.map(&WHAT); # OUTPUT: «((Str) (Str))␤»
68-
say $n.values.perl; # OUTPUT: «(1, 4).Seq␤»
75+
say $m.perl; # OUTPUT: «("b"=>1,"a"=>1,"c"=>2).BagHash␤»
76+
say $n.perl; # OUTPUT: «("b"=>1,"c"=>4).BagHash␤»
6977
7078
=head1 Operators
7179

0 commit comments

Comments
 (0)