Permalink
Browse files

classify

  • Loading branch information...
1 parent 1bb2883 commit 9d969c02c0857538a45e867821b1ccf05d859a6b @moritz moritz committed Jul 11, 2011
Showing with 16 additions and 2 deletions.
  1. +4 −0 src/core/Any.pm
  2. +10 −0 src/core/List.pm
  3. +2 −2 t/spectest.data
View
@@ -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 }
@@ -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) }
View
@@ -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(*);
View
@@ -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

0 comments on commit 9d969c0

Please sign in to comment.