categorize-list
ErrorsCollection

categorize-list

Synthesised documentation from type/Baggy type/Hash

From type/Baggy

See Original text in context

multi method categorize-list(&mapper*@list --> Baggy:D)
multi method categorize-list(%mapper*@list --> Baggy:D)
multi method categorize-list(@mapper*@list --> Baggy:D)

Populates a mutable Baggy by categorizing the possibly-empty @list of values using the given mapper. The @list cannot be lazy.

say BagHash.new.categorize-list: {
    gather {
        take 'largish' if $_ > 5;
        take .is-prime ?? 'prime' !! 'non-prime';
        take $_ %% 2   ?? 'even'  !! 'odd';
    }
}^10;
# OUTPUT: BagHash(largish(4) even(5) non-prime(6) prime(4) odd(5)) 
 
my %mapper = :sugar<sweet white>:lemon<sour>:cake('sweet''is-a-lie');
say MixHash.new.categorize-list: %mapper, <sugar lemon cake>;
# OUTPUT: MixHash(is-a-lie sour white sweet(2))

The mapper can be a Callable that takes a single argument, an Associative, or an Iterable. With Associative and an Iterable mappers, the values in the @list represent the key and index of the mapper's value respectively. A Callable mapper will be executed once per each item in the @list, with that item as the argument and its return value will be used as the mapper's value.

The mapper's value is used as a possibly-empty list of keys of the Baggy that will be incremented by 1.

Note: unlike the Hash's .categorize-list, returning a list of Iterable as mapper's value will throw, as Baggy types do not support nested categorization. For the same reason, Baggy's .categorize-list does not accept :&as parameter.

From type/Hash

See Original text in context

multi method categorize-list(&mapper*@list:&as --> Hash:D)
multi method categorize-list(%mapper*@list:&as --> Hash:D)
multi method categorize-list(@mapper*@list:&as --> Hash:D)

Populates a Hash by classifying the possibly-empty @list of values using the given mapper, optionally altering the values using the :&as Callable. The @list cannot be lazy.

The mapper can be a Callable that takes a single argument, an Associative, or an Iterable. With Associative and an Iterable mappers, the values in the @list represent the key and index of the mapper's value respectively. A Callable mapper will be executed once per each item in the @list, with that item as the argument and its return value will be used as the mapper's value.