Skip to content

Commit 048d6e0

Browse files
committed
Adds example for classify refs #1731
1 parent 782a7de commit 048d6e0

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

doc/Type/Any.pod6

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,8 +891,23 @@ Defined as:
891891
multi sub classify($test, +items, :$into!, *%named )
892892
multi sub classify($test, +items, *%named )
893893
894-
The two first forms will fail.
894+
The two first forms will fail. The rest include a C<$test>, which is a function
895+
that will return a scalar for every input; these will be used as keys of a hash
896+
whose values will be arrays with the elements that output that key for the test
897+
function.
895898
899+
=for code
900+
my @years = (2003..2008).map( { Date.new( $_~"-01-01" ) } );
901+
@years.classify( *.is-leap-year , into => my %leap-years );
902+
say %leap-years;
903+
# OUTPUT: «{False => [2003-01-01 2005-01-01 2006-01-01 2007-01-01],
904+
# True => [2004-01-01 2008-01-01]}␤»
905+
906+
907+
Similarly to L<C<.categorize>|/type/Any#method_categorize>, elements can be
908+
normalized by the C<Callable> passed with the C<:as> argument, and it can use
909+
the C<:into> named argument to pass a C<Hash> the results will be classified
910+
into; in the example above, it's defined on the fly.
896911
897912
=head2 method produce
898913

0 commit comments

Comments
 (0)