@@ -50,10 +50,18 @@ say $breakfast.kxxv.sort; # OUTPUT: «eggs sausage sausage spam spam spam spam
50
50
C < BagHash > es can be composed using C < BagHash.new > . Any positional parameters,
51
51
regardless of their type, become elements of the bag:
52
52
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»
57
65
58
66
Alternatively, the C < .BagHash > coercer (or its functional form, C < BagHash() > )
59
67
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,
62
70
although for Hash-like objects or Pair items, only the keys become elements of
63
71
the bag, and the (cumulative) values become the associated integer weights:
64
72
73
+ my $m = ("a", "b", "c", "c").BagHash;
65
74
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»
69
77
70
78
= head1 Operators
71
79
0 commit comments