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

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(even(5) odd(5)) 
 
my @mapper = <zero one two three four five>;
say MixHash.new.classify-list: @mapper123446;
# OUTPUT: MixHash((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.

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; this Callable is guaranteed to be called only once per item. 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] 
#     } 
# }

In the case we are using Iterables and not Callables, 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.

my @mapper = [['1a','1b','1c'],['2a','2b','2c'],['3a','3b','3c']];
say % .classify-list: @mapper1,2,1,1,2,0;
# OUTPUT: «{1a => {1b => {1c => [0]}}, 2a => {2b => {2c => [1 1 1]}}, 3a => {3b => {3c => [2 2]}}}␤» 

Every element of the array represents a different level in the tree, with the elements of the list that is being mapped used as index, and the elements of the mapper array used as keys to the different levels. So 0 selects the first sub-array and then the subsequent levels are built by running over the rest of the elements of that sub-array.

my @mapper = [['1a','1b'],['2a','2b'],['3a','3b']];
say % .classify-list: @mapper1,0,1,1,1,0,2;
# OUTPUT: «{1a => {1b => [0 0]}, 2a => {2b => [1 1 1 1]}, 3a => {3b => [2]}}␤» 

From version 6.d, trying to use Iterables of different size will throw an error:

my @mapper = [<1a 1b>, <2a 2b 2fail>];
say % .classify-list: @mapper, 1,0,1,1,1,0;
# OUTPUT: «mapper on classify-list computed to an item with different number
# of elements in it than previous items, which cannot be used because all
# values need to have the same number of elements. Mixed-level classification
# is not supported.␤  in block <unit>…»

:&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'] 
# }