routine categorize

Documentation for routine categorize assembled from the following types:

class Any

From Any

(Any) method categorize

Defined as:

method categorize(&mapper --> Hash:D)

Coerces the invocant to a list by applying its .list method and uses List.categorize on it.

say Any.categorize({ $_ }); # OUTPUT: «{(Any) => [(Any)]}␤» 

class List

From List

(List) routine categorize

Defined as:

sub    categorize(&mapper*@values --> Hash:D)
method categorize(List:D: &mapper   --> Hash:D)

Transforms a list of values into a hash representing the categorizations of those values according to &mapper; each hash key represents one possible categorization for one or more of the incoming list values, and the corresponding hash value contains an array of those list values categorized by the mapper into the category of the associated key.

Note that, unlike classify, which assumes that the return value of the mapper is a single value, categorize always assumes that the return value of the mapper is a list of categories that are appropriate to the current value.


sub mapper(Int $ireturns List {
    $i %% 2 ?? 'even' !! 'odd',
    $ ?? 'prime' !! 'not prime'
say categorize &mapper, (17632);  # OUTPUT: «{even => [6 2], not prime => [1 6], 
                                          #          odd => [1 7 3], prime => [7 3 2]}␤»