method classify-list

Documentation for method classify-list assembled from the following types:

class Hash

From Hash

(Hash) method classify-list

Defined as:

multi method classify-list(&mapper*@list:&as --> Hash:D)
multi method classify-list(%mapper*@list:&as --> Hash:D)
multi method classify-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.

Simple classification

In simple classification mode, each mapper's value is any non-Iterable and represents a key to classify @list's item under:

say % .classify-list: { $_ %% 2 ?? 'even' !! 'odd' }^10;
# OUTPUT: «{even => [0 2 4 6 8], odd => [1 3 5 7 9]}␤» 
 
my @mapper = <zero one two three four five>;
my %hash = foo => 'bar';
say %hash.classify-list: @mapper12344;
# OUTPUT: «{foo => bar, four => [4 4], one => [1], three => [3], two => [2]}␤» 

The mapper's value is used as the key of the Hash to which the @list's item will be pushed. See .categorize-list if you wish to classify an item into multiple categories at once.

Multi-level classification

In multi-level classification mode, each mapper's value is an Iterable that represents a tree of hash keys to classify @list's item under:

say % .classify-list: {
    [
        (.is-prime ?? 'prime' !! 'non-prime'),
        ($_ %% 2   ?? 'even'  !! 'odd'      ),
    ]
}^10;
# OUTPUT: 
# { 
#     non-prime => { 
#         even => [0 4 6 8], 
#         odd  => [1 9] 
#     }, 
#     prime => { 
#         even => [2], 
#         odd  => [3 5 7] 
#     } 
# }

NOTE: each of those Iterables must have the same number of elements, or the method will throw an exception. This restriction exists to avoid conflicts when the same key is a leaf of one value's classification but a node of another value's classification.

:&as value modifier

If :&as Callable argument is specified, it will be called once per each item of @list, with the value as the argument, and its return value will be used instead of the original @list's item:

say % .classify-list: :as{"Value is $_"}{ $_ %% 2 ?? 'even' !! 'odd' }^5;
# OUTPUT (slightly altered manually, for clarity): 
# { 
#     even => ['Value is 0', 'Value is 2', 'Value is 4'], 
#     odd  => ['Value is 1', 'Value is 3'] 
# }

role Baggy

From Baggy

(Baggy) method classify-list

Defined as:

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

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

say BagHash.new.classify-list: { $_ %% 2 ?? 'even' !! 'odd' }^10;
# OUTPUT: BagHash.new(even(5), odd(5)) 
 
my @mapper = <zero one two three four five>;
say MixHash.new.classify-list: @mapper123446;
# OUTPUT: MixHash.new((Any), two, three, four(2), one)

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 the key of the Baggy that will be incremented by 1. See .categorize-list if you wish to classify an item into multiple categories at once.

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