Skip to content

Commit

Permalink
Get most of categorize working again on GLR.
Browse files Browse the repository at this point in the history
This is somewhat hacky, definitely needs work on the more
complicated case, probably could use some efficiency improvements
to bring it back in line with the nom version.
  • Loading branch information
colomon committed Aug 27, 2015
1 parent 2023f02 commit ab4d41f
Showing 1 changed file with 35 additions and 33 deletions.
68 changes: 35 additions & 33 deletions src/core/Hash.pm
Expand Up @@ -184,39 +184,41 @@ my class Hash { # declared in BOOTSTRAP
proto method categorize-list(|) { * }
# XXX GLR possibly more efficient taking an Iterable, not a @list
# XXX GLR replace p6listitems op use
#multi method categorize-list( &test, @list, :&as ) {
# fail X::Cannot::Lazy.new(:action<categorize>) if @list.is-lazy;
# if @list {
#
# # multi-level categorize
# if nqp::istype(test(@list[0])[0],List) {
# @list.map: -> $l {
# my $value := &as ?? as($l) !! $l;
# for test($l) -> $k {
# my @keys = @($k);
# my $last := @keys.pop;
# my $hash = self;
# $hash = $hash{$_} //= self.new for @keys;
# nqp::push(
# nqp::p6listitems(nqp::decont($hash{$last} //= [])),
# $value
# );
# }
# }
# }
#
# # just a simple categorize
# else {
# @list.map: -> $l {
# my $value := &as ?? as($l) !! $l;
# nqp::push(
# nqp::p6listitems(nqp::decont(self{$_} //= [])), $value )
# for test($l);
# }
# }
# }
# self;
#}
# XXX GLR I came up with a simple workaround for the main case,
# but it can probably be done more efficiently better.
# Weird case is completely commented out at the moment.
multi method categorize-list( &test, @list, :&as ) {
fail X::Cannot::Lazy.new(:action<categorize>) if @list.is-lazy;
if @list {
# multi-level categorize
if nqp::istype(test(@list[0])[0],Iterable) {
# @list.map: -> $l {
# my $value := &as ?? as($l) !! $l;
# for test($l) -> $k {
# my @keys = @($k);
# my $last := @keys.pop;
# my $hash = self;
# $hash = $hash{$_} //= self.new for @keys;
# $hash{$last}.push: $value;
# }
# }
} else {
# just a simple categorize
@list.map: -> $l {
my $value := &as ?? as($l) !! $l;
(self{$_} //= []).push: $value for test($l);
}
# more efficient (maybe?) nom version that might
# yet be updated for GLR
# @list.map: -> $l {
# my $value := &as ?? as($l) !! $l;
# nqp::push(
# nqp::p6listitems(nqp::decont(self{$_} //= [])), $value )
# for test($l);
}
}
self;
}
multi method categorize-list( %test, $list ) {
self.categorize-list( { %test{$^a} }, $list );
}
Expand Down

0 comments on commit ab4d41f

Please sign in to comment.