Skip to content

Commit b4575c9

Browse files
committed
Document Hash.classify-list
Closes #916
1 parent eee5cfa commit b4575c9

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

doc/Type/Hash.pod6

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,74 @@ It is not, however, possible to do in-place editing of hash keys, even in the ca
311311
312312
=head1 Methods
313313
314+
=head2 method classify-list
315+
316+
Defined as:
317+
318+
multi method classify-list(&mapper, *@list, :&as) returns Hash:D
319+
multi method classify-list(%mapper, *@list, :&as) returns Hash:D
320+
multi method classify-list(@mapper, *@list, :&as) returns Hash:D
321+
322+
Populates a L«C<Hash>|/type/Hash by classifying the
323+
possibly-empty C<@list> of values using the given C<mapper>, optionally
324+
altering the values using the C<:&as> L«C<Callable>|/type/Callable». The
325+
C<@list> cannot be lazy.
326+
327+
The mapper can be a L«C<Callable>|/type/Callable» that takes a single argument,
328+
an L«C<Associative>|/type/Associative», or an L«C<Iterable>|/type/Iterable».
329+
With L«C<Associative>|/type/Associative» and an L«C<Iterable>|/type/Iterable»
330+
mappers, the values in the C<@list> represent the key and index of the mapper's
331+
value respectively. A L«C<Callable>|/type/Callable» mapper will be executed
332+
once per each item in the C<@list>, with that item as the argument and its
333+
return value will be used as the mapper's value.
334+
335+
=head3 Simple Classification
336+
337+
In simple classification mode, each mapper's value is any non-Iterable and
338+
represents a key to classify C<@list>'s item under:
339+
340+
say % .classify-list: { $_ %% 2 ?? 'even' !! 'odd' }, ^10;
341+
# OUTPUT: {even => [0 2 4 6 8], odd => [1 3 5 7 9]}
342+
343+
my @mapper = <zero one two three four five>;
344+
my %hash = foo => 'bar';
345+
say %hash.classify-list: @mapper, 1, 2, 3, 4, 4;
346+
# OUTPUT: {foo => bar, four => [4 4], one => [1], three => [3], two => [2]}
347+
348+
The mapper's value is used as the key of the L«C<Hash>|/type/Hash» to
349+
which the C<@list>'s item will be L«C<push>ed|/routine/push». See
350+
L«C<.categorize-list>|/routine/categorize-list» if you wish to classify an item
351+
into multiple categories at once.
352+
353+
=head3 Multi-Level Classification
354+
355+
In multi-level classification mode, each mapper's value is an
356+
L«C<Iterable>|/type/Iterable» that represents a tree of hash keys to classify
357+
C<@list>'s item under:
358+
359+
say % .classify-list: {
360+
[
361+
(.is-prime ?? 'prime' !! 'non-prime'),
362+
($_ %% 2 ?? 'even' !! 'odd' ),
363+
]
364+
}, ^10;
365+
# OUTPUT:
366+
# {
367+
# non-prime => {
368+
# even => [0 4 6 8],
369+
# odd => [1 9]
370+
# },
371+
# prime => {
372+
# even => [2],
373+
# odd => [3 5 7]
374+
# }
375+
# }
376+
377+
B<NOTE:> each of those L«C<Iterables>|/type/Iterable»
378+
must have the same number of elements, or the method will throw an exception.
379+
This restriction exists to avoid conflicts when the same key is a
380+
leaf of one value's classification but a node of another value's classification.
381+
314382
=head2 method push
315383
316384
Defined as:

0 commit comments

Comments
 (0)