Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
classify
  • Loading branch information
moritz committed Jul 11, 2011
1 parent 1bb2883 commit 9d969c0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/core/Any.pm
Expand Up @@ -10,6 +10,7 @@ my class Any {
method eager() { nqp::p6list(nqp::list(self), List, Bool::True).eager }
method elems() { self.list.elems }
method end() { self.list.end }
method classify(&t) { self.list.classify(&t) }
method infinite() { Mu }
method flat() { nqp::p6list(nqp::list(self), List, Bool::True) }
method hash() { my %h = self }
Expand Down Expand Up @@ -195,3 +196,6 @@ multi elems($a) { $a.elems }

proto end(|$) { * }
multi end($a) { $a.end }

proto classify(|$) { * }
multi classify(&test, *@items) { @items.classify(&test) }
10 changes: 10 additions & 0 deletions src/core/List.pm
Expand Up @@ -189,6 +189,16 @@ class List does Positional {
self[$index];
}

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

multi method gist(List:D:) { self.Str }
multi method perl(List:D \$self:) {
self.gimme(*);
Expand Down
4 changes: 2 additions & 2 deletions t/spectest.data
Expand Up @@ -493,9 +493,9 @@ S32-hash/exists.t
# S32-io/file-tests.t # err: Method 'IO' not found for invocant of class 'Str'
# S32-io/note.t # err: Could not find sub &DYNAMIC
# S32-io/IO-Socket-INET.t # err: ResizablePMCArray: Can't shift from an empty array!
# S32-list/classify.t # err: Method 'classify' not found for invocant of class 'Array'
S32-list/classify.t
S32-list/create.t
S32-list/end.t # err: Method 'end' not found for invocant of class 'Array'
S32-list/end.t
# S32-list/first.t # err: too many positional arguments: 3 passed, 1 expected
S32-list/grep.t
S32-list/join.t
Expand Down

0 comments on commit 9d969c0

Please sign in to comment.