Forked from dogbert17/gist:c0b286f90bed38f83397f8f431f22d30
Last active
June 6, 2016 09:29
-
-
Save jast/45e6533056692a81d35e56a0e83b62df to your computer and use it in GitHub Desktop.
Attempt to document List.categorize, most of the text from https://raw.githubusercontent.com/perl6/specs/master/S32-setting-library/Containers.pod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=head2 routine categorize | |
Defined as: | |
multi sub categorize(&mapper, *@values) returns Hash:D | |
multi method categorize(List:D: &mapper) returns Hash:D | |
Usage: | |
categorize MAPPER, LIST | |
LIST.categorize(MAPPER) | |
Transforms a list into a hash of arrays, based on categories determined | |
by a mapper function. The mapper receives a value as its argument and | |
returns a list of categories. In the resulting hash, each key will be a | |
category and its value will be an array of corresponding values from the | |
original list. | |
Note that, unlike L<classify>, which assumes that the return value | |
of the mapper is a single value, C<categorize> always assumes that | |
the return value of the mapper is a list of categories that are | |
appropriate to the current value. | |
Example: | |
sub mapper(Int $i) returns List { | |
$i %% 2 ?? 'even' !! 'odd', | |
$i.is-prime ?? 'prime' !! 'not prime' | |
} | |
say categorize &mapper, (1, 7, 6, 3, 2); # {even => [2], odd => [7 3], prime => [7 3 2]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment