Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Example for categorize refs #1731
  • Loading branch information
JJ committed Feb 19, 2019
1 parent b8873a3 commit 782a7de
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions doc/Type/Any.pod6
Expand Up @@ -860,17 +860,39 @@ say %leap-years
# => [2002-01-01 2003-01-01 2005-01-01 2006-01-01 2007-01-01 2009-01-01],
# True => [2004-01-01 2008-01-01]}␤»
The function used to categorize can return an array indicating all possible bins
their argument can be put into:
=begin code
sub divisible-by( Int $n --> Array(Seq) ) {
gather {
for <2 3 5 7> {
take $_ if $n %% $_;
}
}
}
say (3..13).categorize( &divisible-by );
# OUTPUT:
# «{2 => [4 6 8 10 12], 3 => [3 6 9 12], 5 => [5 10], 7 => [7]}␤»
=end code
In this case, every number in the range is classified in as many bins as it can
be divided by.
=head2 method classify
Defined as:
method classify(&mapper -->Hash:D)
multi method classify()
multi method classify(Whatever)
multi method classify($test, :$into!, :&as)
multi method classify($test, :&as)
multi sub classify($test, +items, :$into!, *%named )
multi sub classify($test, +items, *%named )
Coerces the invocant to a C<list> by applying
its L«C<.list>|/routine/list» method and uses
L«C<List.classify>|/type/List#routine_classify» on it.
The two first forms will fail.
say Any.classify({ $_ }); # OUTPUT: «{(Any) => [(Any)]}␤»
=head2 method produce
Expand Down

0 comments on commit 782a7de

Please sign in to comment.