Skip to content

Commit

Permalink
[src/core/List.pm] categorize() method and sub, add test to spectest.…
Browse files Browse the repository at this point in the history
…data
  • Loading branch information
mberends committed Aug 29, 2011
1 parent 1848338 commit ebd4957
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/core/List.pm
Expand Up @@ -239,6 +239,18 @@ my class List does Positional {
%result.pairs;
}

method categorize(&test) {
my %result;
for @.list {
my @k = test $_;
for @k -> $k {
%result{$k} //= [];
%result{$k}.push: $_;
}
}
%result.pairs;
}

multi method gist(List:D:) { self.Str }
multi method perl(List:D \$self:) {
self.gimme(*);
Expand Down Expand Up @@ -329,3 +341,4 @@ multi sub push(@a, *@elems) { @a.push: @elems }
sub reverse(*@a) { @a.reverse }
sub rotate(@a, Int $n = 1) { @a.rotate($n) }
sub reduce (&with, *@list) { @list.reduce(&with) }
sub categorize(&mapper, *@a){ @a.categorize(&mapper)}
1 change: 1 addition & 0 deletions t/spectest.data
Expand Up @@ -500,6 +500,7 @@ S32-io/dir.t
S32-io/file-tests.t
S32-io/note.t
# S32-io/IO-Socket-INET.t # err: Useless declaration of a has-scoped method in mainline
S32-list/categorize.t
S32-list/classify.t
S32-list/create.t
S32-list/end.t
Expand Down

0 comments on commit ebd4957

Please sign in to comment.