method classify-list
1 | class Hash |
1.1 | (Hash) method classify-list |
1.1.1 | Simple classification |
1.1.2 | Multi-level classification |
1.1.3 | :&as value modifier |
2 | role Baggy |
2.1 | (Baggy) 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(, *, : --> Hash)multi method classify-list(, *, : --> Hash)multi method classify-list(, *, : --> Hash)
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: , ^10;# OUTPUT: «{even => [0 2 4 6 8], odd => [1 3 5 7 9]}»my = <zero one two three four five>;my = foo => 'bar';say .classify-list: , 1, 2, 3, 4, 4;# 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 push
ed. 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:, ^10;# OUTPUT:# {# non-prime => {# even => [0 4 6 8],# odd => [1 9]# },# prime => {# even => [2],# odd => [3 5 7]# }# }
NOTE: each of those Iterable
s 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, , ^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(, * --> Baggy)multi method classify-list(, * --> Baggy)multi method classify-list(, * --> Baggy)
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: , ^10;# OUTPUT: BagHash.new(even(5), odd(5))my = <zero one two three four five>;say MixHash.new.classify-list: , 1, 2, 3, 4, 4, 6;# 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.