Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow multi-level classifications, as per spec

  • Loading branch information...
commit 9dd34f39bae1d715532a577c6e7055e92ef6e6a6 1 parent 5628e68
@lizmat lizmat authored
Showing with 63 additions and 6 deletions.
  1. +63 −6 src/core/Hash.pm
View
69 src/core/Hash.pm
@@ -108,20 +108,77 @@ my class Hash {
proto method classify(|) { * }
multi method classify( &test, *@list ) {
fail 'Cannot .classify an infinite list' if @list.infinite;
- nqp::push( nqp::p6listitems(nqp::decont(self{test $_} //= [])), $_ )
- for @list;
+ if @list {
+
+ # multi-level classify
+ if test(@list[0]) ~~ List {
+ for @list -> $l {
+ my @keys = test($l);
+ my $last := @keys.pop;
+ my $hash = self;
+ $hash = $hash{$_} //= self.new for @keys;
+ nqp::push(
+ nqp::p6listitems(nqp::decont($hash{$last} //= [])), $l );
+ }
+ }
+
+ # just a simple classify
+ else {
+ nqp::push(
+ nqp::p6listitems(nqp::decont(self{test $_} //= [])), $_ )
+ for @list;
+ }
+ }
self;
}
multi method classify( %test, *@list ) {
fail 'Cannot .classify an infinite list' if @list.infinite;
- nqp::push( nqp::p6listitems(nqp::decont(self{%test{$_}} //= [])), $_ )
- for @list;
+ if @list {
+
+ # multi-level classify
+ if %test{@list[0]} ~~ List {
+ for @list -> $l {
+ my @keys = %test{$l};
+ my $last := @keys.pop;
+ my $hash = self;
+ $hash = $hash{$_} //= self.new for @keys;
+ nqp::push(
+ nqp::p6listitems(nqp::decont($hash{$last} //= [])), $l );
+ }
+ }
+
+ # just a simple classify
+ else {
+ nqp::push(
+ nqp::p6listitems(nqp::decont(self{%test{$_}} //= [])), $_ )
+ for @list;
+ }
+ }
self;
}
multi method classify( @test, *@list ) {
fail 'Cannot .classify an infinite list' if @list.infinite;
- nqp::push( nqp::p6listitems(nqp::decont(self{@test[$_]} //= [])), $_ )
- for @list;
+ if @list {
+
+ # multi-level classify
+ if @test[@list[0]] ~~ List {
+ for @list -> $l {
+ my @keys = @test[$l];
+ my $last := @keys.pop;
+ my $hash = self;
+ $hash = $hash{$_} //= self.new for @keys;
+ nqp::push(
+ nqp::p6listitems(nqp::decont($hash{$last} //= [])), $l );
+ }
+ }
+
+ # just a simple classify
+ else {
+ nqp::push(
+ nqp::p6listitems(nqp::decont(self{@test[$_]} //= [])), $_ )
+ for @list;
+ }
+ }
self;
}
Please sign in to comment.
Something went wrong with that request. Please try again.