Skip to content

Commit eee5cfa

Browse files
committed
Improve Baggy.categorize-list
- Document all mappers - Doocument throwage conditions
1 parent 6b99fb8 commit eee5cfa

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

doc/Type/Baggy.pod6

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -227,40 +227,43 @@ does not accept C<:&as> parameter.
227227
228228
Defined as:
229229
230-
multi method categorize-list(&test:(Any --> Bool), *@list) returns Baggy:D
231-
232-
Transforms a list of values into categorizations of those values according to
233-
C<&test> and stores the results in the invocant which is then returned.
234-
Each key represents one possible categorization for one or more of the
235-
incoming list values, and the corresponding value contains the number of list
236-
values categorized by C<&test> into the category of the associated key.
237-
238-
The signature for C<&test> contains one parameter of type C<Any> which simply
239-
means that C<&test> must be able to handle arguments of the same type as those
240-
in C<*@list>.
241-
242-
As an example, suppose that we have a list of C<Int>s each of which is either
243-
even or odd and, at the same time, prime or non-prime. We want to get hold of
244-
an aggregated result which shows us how many numbers were even, odd, prime and
245-
non-prime.
246-
247-
my $b = BagHash.new();
248-
dd $b.categorize-list( { $_ %% 2 ?? 'even' !! 'odd',
249-
$_.is-prime ?? 'prime' !! 'not prime'
250-
}, (1, 7, 6, 3, 2) );
251-
# returns: ("non-prime"=>2,"even"=>2,"prime"=>3,"odd"=>3).BagHash
252-
253-
The result shows us that of the numbers in the list two were categorized as
254-
even and three as odd. In addition the result also shows that three of the numbers
255-
were also categorized as prime and two as non-prime. Note that the result doesn't
256-
show us B<which> numbers were categorized as being even, odd, prime or non-prime.
257-
If that is what you want, use the L<categorize|/type/List#routine_categorize>
258-
routine in L<List|/type/List> instead.
259-
260-
It should also be noted that, unlike L<classify-list|/type/Baggy#method_classify-list>,
261-
which assumes that the return value of C<&test> is a single value, C<categorize-list>
262-
always assumes that the return value of C<&test> is a list of categories that are
263-
appropriate to the current value.
230+
multi method categorize-list(&mapper, *@list) returns Baggy:D
231+
multi method categorize-list(%mapper, *@list) returns Baggy:D
232+
multi method categorize-list(@mapper, *@list) returns Baggy:D
233+
234+
Populates a I<mutable> L«C<Baggy>|/type/Baggy» by categorizing the
235+
possibly-empty C<@list> of values using the given C<mapper>. The C<@list>
236+
cannot be lazy.
237+
238+
say BagHash.new.categorize-list: {
239+
gather {
240+
take 'largish' if $_ > 5;
241+
take .is-prime ?? 'prime' !! 'non-prime';
242+
take $_ %% 2 ?? 'even' !! 'odd';
243+
}
244+
}, ^10;
245+
# OUTPUT: BagHash.new(largish(4), even(5), non-prime(6), prime(4), odd(5))
246+
247+
my %mapper = :sugar<sweet white>, :lemon<sour>, :cake('sweet', 'is a lie');
248+
say MixHash.new.categorize-list: %mapper, <sugar lemon cake>;
249+
# OUTPUT: MixHash.new(is a lie, sour, white, sweet(2))
250+
251+
The mapper can be a L«C<Callable>|/type/Callable» that takes a single argument,
252+
an L«C<Associative>|/type/Associative», or an L«C<Iterable>|/type/Iterable».
253+
With L«C<Associative>|/type/Associative» and an L«C<Iterable>|/type/Iterable»
254+
mappers, the values in the C<@list> represent the key and index of the mapper's
255+
value respectively. A L«C<Callable>|/type/Callable» mapper will be executed
256+
once per each item in the C<@list>, with that item as the argument and its
257+
return value will be used as the mapper's value.
258+
259+
The mapper's value is used as a possibly-empty list of keys of the
260+
L«C<Baggy>|/type/Baggy» that will be incremented by C<1>.
261+
262+
B<Note:> unlike the L«C<Hash>|/type/Hash»'s
263+
C<.categorize-list>, returning a list of L«C<Iterables>|/type/Iterable»
264+
as mapper's value will throw, as L«C<Baggy>|/type/Baggy» types do not support
265+
nested categorization. For the same reason, L«C<Baggy>|/type/Baggy»'s
266+
C<.categorize-list> does not accept C<:&as> parameter.
264267
265268
=head2 method keys
266269

0 commit comments

Comments
 (0)