Skip to content

Commit 031027c

Browse files
authored
Merge pull request #2358 from Coleoid/mixhash-todo
Document the Setty ops for MixHashes per TODO
2 parents f759c7d + efb4151 commit 031027c

File tree

3 files changed

+58
-14
lines changed

3 files changed

+58
-14
lines changed

doc/Language/setbagmix.pod6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ collection.
3131
3232
The types L<Mix|/type/Mix> and L<MixHash|/type/MixHash> are similar
3333
to L<Bag|/type/Bag> and L<BagHash|/type/BagHash>, but they also
34-
allow B<fractional weights>.
34+
allow B<fractional and negative weights>.
3535
3636
=begin comment
3737
=defn Bag or BagHash

doc/Type/BagHash.pod6

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,36 @@ my ($a, $b) = BagHash.new(2, 2, 4), BagHash.new(2, 3, 3, 4);
8686
8787
say $a (<) $b; # OUTPUT: «False␤»
8888
say $a (<+) $b; # OUTPUT: «False␤»
89-
say $a (^) $b; # OUTPUT: «bag(3(2), 2)␤»
90-
say $a (+) $b; # OUTPUT: «bag(2(3), 4(2), 3(2))␤»
89+
say $a (^) $b; # OUTPUT: «Bag(3(2), 2)␤»
90+
say $a (+) $b; # OUTPUT: «Bag(2(3), 4(2), 3(2))␤»
9191
9292
# Unicode versions:
9393
say $a ⊂ $b; # OUTPUT: «False␤»
9494
say $a ≼ $b; # OUTPUT: «False␤»
95-
say $a ⊖ $b; # OUTPUT: «bag(3(2), 2)␤»
96-
say $a ⊎ $b; # OUTPUT: «bag(2(3), 4(2), 3(2))␤»
95+
say $a ⊖ $b; # OUTPUT: «Bag(3(2), 2)␤»
96+
say $a ⊎ $b; # OUTPUT: «Bag(2(3), 4(2), 3(2))␤»
9797
=end code
9898
9999
See L<Set/Bag Operators|/language/setbagmix#Set/Bag_operators> for a complete list of
100100
set and bag operators with detailed explanations.
101101
102102
=head1 Note on C<reverse> and ordering.
103103
104-
This method is inherited from L<Any|/type/Any#routine_reverse>, however, C<Mix>es do not have an inherent order and you should not trust it returning a consistent output.
104+
BagHash inherits C<reverse> from L<Any|/type/Any#routine_reverse>,
105+
however, C<Bag>s do not have an inherent order and you should not trust
106+
it returning a consistent output.
107+
108+
If you sort a BagHash, the result is a list of pairs, at which point
109+
C<reverse> makes perfect sense:
110+
111+
=begin code
112+
my $a = BagHash.new(2, 2, 18, 3, 4);
113+
say $a; # OUTPUT: «BagHash(18, 2(2), 3, 4)␤»
114+
115+
say $a.sort; # OUTPUT: «(2 => 2 3 => 1 4 => 1 18 => 1)␤»
116+
say $a.sort.reverse; # OUTPUT: «(18 => 1 4 => 1 3 => 1 2 => 2)␤»
117+
=end code
118+
105119
106120
=head1 See Also
107121

doc/Type/MixHash.pod6

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,42 @@ the mix, and the (cumulative) values become the associated numeric weights:
6868
6969
=head1 Operators
7070
71-
=begin comment
7271
73-
TODO: Expand this section (using the corresponding section in
74-
lib/Type/BagHash.pod as a guide) after ab5tract's set/bag/mix operator redesign.
75-
76-
=end comment
72+
=begin code
73+
my ($a, $b) = MixHash(2 => 2, 4), MixHash(2 => 1.5, 3 => 2, 4);
74+
75+
say $a (<) $b; # OUTPUT: «False␤»
76+
say $a (<+) $b; # OUTPUT: «False␤»
77+
say $a (^) $b; # OUTPUT: «Mix(2(0.5), 3(2))␤»
78+
say $a (+) $b; # OUTPUT: «Mix(2(3.5), 4(2), 3(2))␤»
79+
80+
# Unicode versions:
81+
say $a ⊂ $b; # OUTPUT: «False␤»
82+
say $a ≼ $b; # OUTPUT: «False␤»
83+
say $a ⊖ $b; # OUTPUT: «Mix(2(0.5), 3(2))␤»
84+
say $a ⊎ $b; # OUTPUT: «Mix(2(3.5), 4(2), 3(2))␤»
85+
=end code
7786
7887
See L<Set/Bag Operators|/language/setbagmix#Set/Bag_operators> for a complete list of set and bag operators
7988
with detailed explanations.
8089
90+
=head1 Note on C<reverse> and ordering.
91+
92+
MixHash inherits C<reverse> from L<Any|/type/Any#routine_reverse>,
93+
however, C<Mix>es do not have an inherent order and you should not trust
94+
it returning a consistent output.
95+
96+
If you sort a MixHash, the result is a list of pairs, at which point
97+
C<reverse> makes perfect sense:
98+
99+
=begin code
100+
my $a = MixHash.new(2, 2, 18, 3, 4);
101+
say $a; # OUTPUT: «MixHash(18, 2(2), 3, 4)␤»
102+
103+
say $a.sort; # OUTPUT: «(2 => 2 3 => 1 4 => 1 18 => 1)␤»
104+
say $a.sort.reverse; # OUTPUT: «(18 => 1 4 => 1 3 => 1 2 => 2)␤»
105+
=end code
106+
81107
=head1 Methods
82108
83109
=head2 method Bag
@@ -86,7 +112,8 @@ Defined as:
86112
87113
method Bag (--> Bag:D)
88114
89-
Coerces the C<MixHash> to a L«C<Bag>|/type/Bag». The weights are convert to L«C<Int>|/type/Int»,
115+
Coerces the C<MixHash> to a L«C<Bag>|/type/Bag». The weights are converted
116+
to L«C<Int>|/type/Int»,
90117
which means the number of keys in the resulting C<Bag> can be fewer than in the
91118
original C<MixHash>, if any of the weights are negative or truncate to zero.
92119
@@ -96,13 +123,16 @@ Defined as:
96123
97124
method BagHash (--> BagHash:D)
98125
99-
Coerces the C<MixHash> to a L«C<BagHash>|/type/BagHash». The weights are convert to L«C<Int>|/type/Int»,
126+
Coerces the C<MixHash> to a L«C<BagHash>|/type/BagHash». The weights are converted
127+
to L«C<Int>|/type/Int»,
100128
which means the number of keys in the resulting C<BagHash> can be fewer than in the
101129
original C<MixHash>, if any of the weights are negative or truncate to zero.
102130
103131
=head1 Note on C<reverse> and ordering
104132
105-
This method is inherited from L<Any|/type/Any#routine_reverse>, however, C<Mix>es do not have an inherent order and you should not trust it returning a consistent output.
133+
This method is inherited from L<Any|/type/Any#routine_reverse>, however,
134+
C<Mix>es do not have an inherent order and you should not trust it returning
135+
a consistent output.
106136
107137
=head1 See Also
108138

0 commit comments

Comments
 (0)