diff --git a/Changes b/Changes index 364bb0db..33891579 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,7 @@ Revision history for Red {{$NEXT}} - Fix test on Pg + - Accept .classify with multiple columns 0.1.66 2022-12-18T21:55:33Z - Add `is view` diff --git a/lib/Red/ResultAssociative.pm6 b/lib/Red/ResultAssociative.pm6 index ca08135c..99e9575a 100644 --- a/lib/Red/ResultAssociative.pm6 +++ b/lib/Red/ResultAssociative.pm6 @@ -11,6 +11,7 @@ unit role Red::ResultAssociative[$of, Red::AST $key-of] does Associative; has Red::AST $!key-of = $key-of; has $.rs is required; +has @.next-level; #| type of the value method of { $of } @@ -37,7 +38,12 @@ method elems { #| return a ResultSeq for the given key method AT-KEY($key) { - $!rs.grep: { Red::AST::Eq.new: $!key-of, ast-value($key), :bind-right } + my \resultseq = $!rs.grep: { Red::AST::Eq.new: $!key-of, ast-value($key), :bind-right } + do if @!next-level { + Red::ResultAssociative[$of, @!next-level.head].new: :rs(resultseq), |(:next-level(@!next-level.skip) if @!next-level.elems > 1) + } else { + resultseq + } } method iterator { diff --git a/lib/Red/ResultSeq.pm6 b/lib/Red/ResultSeq.pm6 index 931f4ffc..25f791dd 100644 --- a/lib/Red/ResultSeq.pm6 +++ b/lib/Red/ResultSeq.pm6 @@ -288,7 +288,7 @@ method classify(\SELF: &func, :&as = { $_ } --> Red::ResultAssociative) is hidde } else { my \key = func SELF.of; my \value = as SELF.of; - Red::ResultAssociative[value, key].new: :rs(SELF) + Red::ResultAssociative[value, key.head].new: :rs(SELF), |(:next-level(key.skip) if key.elems > 1) } } diff --git a/t/27-classify.t b/t/27-classify.t index c3544f4f..3f0b5320 100644 --- a/t/27-classify.t +++ b/t/27-classify.t @@ -42,4 +42,14 @@ is-deeply Bla.^all.classify(*.bla).Set, set(); is-deeply Bla.^all.map(*.bla).Bag, bag(); is-deeply Bla.^all.map(*.bla).Set, set(); +my %d := Bla.^all.classify({ .bla, .id }); +is %d.elems, 2; +isa-ok %d, Red::ResultAssociative; +is %d.elems, 2; +is %d.keys, <1 2>; + +isa-ok %d, Red::ResultAssociative; +is %d.elems, 1; +is %d.keys, <3>; + done-testing;