Skip to content

Commit dcdaddc

Browse files
committed
Comments on key/value ordering closes #2182
1 parent 2c831c2 commit dcdaddc

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

doc/Type/Hash.pod6

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ C<Hash> implements C<Associative> through its inheritance of C<Map> and as such
1212
provides support for looking up values using keys, providing support for
1313
L<associative subscripting|/language/subscripts#Methods_to_implement_for_associative_subscripting>.
1414
15+
Although the order of the hashes is guaranteed to be random in every single
16+
call, still successive calls to C<.keys> and C<.values> are guaranteed to return
17+
them in the same order:
18+
19+
my %orig = :1a, :2b; my %new = :5b, :6c;
20+
%orig{ %new.keys } = %new.values;
21+
say %orig.perl; # OUTPUT: «{:a(1), :b(5), :c(6)}␤»
22+
23+
In this case, C<b> will always be associated to 5 and C<c> to 6; even if two
24+
succesive calls to C<keys> will return them in different order. Successive calls
25+
to any of them separately and repeatedly will always return the same order in
26+
any program invocation.
27+
1528
=head1 Methods
1629
1730
=head2 method classify-list
@@ -35,7 +48,7 @@ value respectively. A L«C<Callable>|/type/Callable» mapper will be executed
3548
once per each item in the C<@list>, with that item as the argument and its
3649
return value will be used as the mapper's value.
3750
38-
=head3 Simple Classification
51+
=head3 Simple classification
3952
4053
In simple classification mode, each mapper's value is any non-Iterable and
4154
represents a key to classify C<@list>'s item under:
@@ -53,7 +66,7 @@ which the C<@list>'s item will be L«C<push>ed|/routine/push». See
5366
L«C<.categorize-list>|/routine/categorize-list» if you wish to classify an item
5467
into multiple categories at once.
5568
56-
=head3 Multi-Level Classification
69+
=head3 Multi-level classification
5770
5871
In multi-level classification mode, each mapper's value is an
5972
L«C<Iterable>|/type/Iterable» that represents a tree of hash keys to classify
@@ -116,7 +129,7 @@ value respectively. A L«C<Callable>|/type/Callable» mapper will be executed
116129
once per each item in the C<@list>, with that item as the argument and its
117130
return value will be used as the mapper's value.
118131
119-
=head3 Simple Categorization
132+
=head3 Simple categorization
120133
121134
The mapper's value is expected to be a possibly empty list of
122135
non-L«C<Iterables>|/type/Iterable» that represent categories to place the value
@@ -140,7 +153,7 @@ into:
140153
141154
Notice how some items, e.g. C<6> and C<7>, are present in several categories.
142155
143-
=head3 Multi-Level Categorization
156+
=head3 Multi-level categorization
144157
145158
In multi-level categorization, the categories produced by the mapper can are
146159
L<Iterables|/type/Iterable> and categorization combines features
@@ -212,11 +225,11 @@ of the old value.
212225
Example:
213226
214227
my %h = a => 1;
215-
%h.push: (a => 1); # a => [1,1]
216-
%h.push: (a => 1) xx 3 ; # a => [1,1,1,1,1]
217-
%h.push: (b => 3); # a => [1,1,1,1,1], b => 3
218-
%h.push('c' => 4); # a => [1,1,1,1,1], b => 3, c => 4
219-
push %h, 'd' => 5; # a => [1,1,1,1,1], b => 3, c => 4, d => 5
228+
%h.push: (a => 1); # a => [1,1]
229+
%h.push: (a => 1) xx 3 ; # a => [1,1,1,1,1]
230+
%h.push: (b => 3); # a => [1,1,1,1,1], b => 3
231+
%h.push('c' => 4); # a => [1,1,1,1,1], b => 3, c => 4
232+
push %h, 'd' => 5; # a => [1,1,1,1,1], b => 3, c => 4, d => 5
220233
221234
Please note that C<Pair>s or
222235
L<colon pairs|/language/glossary#index-entry-Colon_Pair> as arguments to push
@@ -234,7 +247,7 @@ index:
234247
235248
my %wc = 'hash' => 323, 'pair' => 322, 'pipe' => 323;
236249
(my %inv).push: %wc.invert;
237-
say %inv; # OUTPUT: «{322 => pair, 323 => [pipe hash]}␤»
250+
say %inv; # OUTPUT: «{322 => pair, 323 => [pipe hash]}␤»
238251
239252
Note that such a initialization could also be written as
240253
@@ -364,8 +377,8 @@ Note that in the L<Scalar> case you have to use the C<VAR> method in
364377
order to get correct information.
365378
366379
my $s is dynamic = %('apples' => 5);
367-
say $s.dynamic; # OUTPUT: «False␤» (wrong, don't do this)
368-
say $s.VAR.dynamic; # OUTPUT: «True␤» (correct approach)
380+
say $s.dynamic; # OUTPUT: «False␤» (wrong, don't do this)
381+
say $s.VAR.dynamic; # OUTPUT: «True␤» (correct approach)
369382
370383
=head1 Subscript Adverbs
371384
@@ -375,7 +388,8 @@ for more information).
375388
376389
=head2 C<:exists>
377390
378-
The adverb C<:exists> returns C<Bool::True> if a key exists in the Hash. If more than one key is supplied it returns a C<List> of C<Bool>.
391+
The adverb C<:exists> returns C<Bool::True> if a key exists in the Hash. If more
392+
than one key is supplied it returns a C<List> of C<Bool>.
379393
380394
my %h = a => 1, b => 2;
381395
say %h<a>:exists; # OUTPUT: «True␤»
@@ -395,7 +409,8 @@ Use C<:delete> to remove a C<Pair> from the C<Hash>.
395409
396410
=head2 C<:p>
397411
398-
The adverb C<:p> returns a C<Pair> or a List of C<Pair> instead of just the value.
412+
The adverb C<:p> returns a C<Pair> or a List of C<Pair> instead of just the
413+
value.
399414
400415
my %h = a => 1, b => 2;
401416
say %h<a>:p; # OUTPUT: «a => 1␤»

0 commit comments

Comments
 (0)