Skip to content

Commit

Permalink
Accept multiple columns on .classify. Related to #501
Browse files Browse the repository at this point in the history
  • Loading branch information
FCO committed Dec 24, 2022
1 parent 700e465 commit 3790d02
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
8 changes: 7 additions & 1 deletion lib/Red/ResultAssociative.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/Red/ResultSeq.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
10 changes: 10 additions & 0 deletions t/27-classify.t
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ is-deeply Bla.^all.classify(*.bla).Set, set(<test1 test2>);
is-deeply Bla.^all.map(*.bla).Bag, bag(<test1 test1 test2>);
is-deeply Bla.^all.map(*.bla).Set, set(<test1 test2>);

my %d := Bla.^all.classify({ .bla, .id });
is %d.elems, 2;
isa-ok %d<test1>, Red::ResultAssociative;
is %d<test1>.elems, 2;
is %d<test1>.keys, <1 2>;

isa-ok %d<test2>, Red::ResultAssociative;
is %d<test2>.elems, 1;
is %d<test2>.keys, <3>;

done-testing;

0 comments on commit 3790d02

Please sign in to comment.