From 916b56d60d40d02f3e620d8857d94582b896c1ae Mon Sep 17 00:00:00 2001 From: Moritz Lenz Date: Fri, 7 May 2010 23:27:05 +0200 Subject: [PATCH] implement List.classify; all tests pass except those depending on binding --- src/core/Any-list.pm | 11 +++++++++++ t/spectest.data | 1 + 2 files changed, 12 insertions(+) diff --git a/src/core/Any-list.pm b/src/core/Any-list.pm index da8694c5f8c..4565b912f27 100644 --- a/src/core/Any-list.pm +++ b/src/core/Any-list.pm @@ -175,6 +175,16 @@ augment class Any { self.pick(Inf, :$replace); } + multi method classify($test) { + my %result; + for @.list { + my $k = $_ ~~ $test; + %result{$k} //= []; + %result{$k}.push: $_; + } + %result.pairs; + } + multi method reduce(Code $expression is rw) { my $arity = $expression.?count || 2; # second half is a CHEAT fail('Cannot reduce() using a unary or nullary function.') @@ -247,6 +257,7 @@ augment class Any { } } +proto sub classify($matcher, *@values) { @values.classify($matcher) } proto sub join (Str $separator = '', *@values) { @values.join($separator); } proto sub reverse(@values) { @values.reverse; } multi sub reverse(*@v) { @v.reverse; } diff --git a/t/spectest.data b/t/spectest.data index 78fa6bb86b5..ea6cb87223c 100644 --- a/t/spectest.data +++ b/t/spectest.data @@ -471,6 +471,7 @@ S32-hash/push.t S32-io/file-tests.t S32-io/note.t # S32-io/IO-Socket-INET.t +S32-list/classify.t ## S32-list/create.t S32-list/end.t S32-list/first.t