Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jast/45e6533056692a81d35e56a0e83b62df to your computer and use it in GitHub Desktop.
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
=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