Skip to content

Commit

Permalink
Allow multi-level classifications, as per spec
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Jul 29, 2013
1 parent 5628e68 commit 9dd34f3
Showing 1 changed file with 63 additions and 6 deletions.
69 changes: 63 additions & 6 deletions src/core/Hash.pm
Expand Up @@ -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;
}

Expand Down

0 comments on commit 9dd34f3

Please sign in to comment.