here is an MRE (showing two attempts, with debug left in to be helpful) to try and get 2d subscripting working with AT-POS across a DataFrame that has columns of Series...
class Series does Positional {
has Real @.data = [0.1,0.2,0.3];
method AT-POS( $p ) {
@!data[$p]
}
}
class DataFrame does Positional {
has Series @.series;
#`[ ATTEMPT #1
method AT-POS( $p, $q? ) {
given $q {
when Int { #say 'Int';
@!series[$p][$q]
}
when Whatever { #say '*';
@!series[$p].data
}
default { #say 'default';
@!series[$p]
}
}
}
#]
# ATTEMPT #2
method AT-POS(|c) is raw { #`[dd c;] @!series.AT-POS(|c) }
}
my $df = DataFrame.new( series => [Series.new xx 3] );
say $df[1].data; #[0.1 0.2 0.3]
say $df[1][2]; #0.3
say $df[0,1]; #(Series.new(data => $[0.1, 0.2, 0.3]) Series.new(data => $[0.1, 0.2, 0.3]))
say $df[1;2]; #0.3
say $df[1;*]; #got (0.1) ... expected [0.1 0.2 0.3]
say $df[*;1]; #got (0.2) ... wanted [0.2 0.2 0.2]
I already researched on SO and found three related questions here, here and here ... and the attempt #2 in my code seeks to apply @lizmats Answer to the third one. Encouragingly both the attempts in my MRE have the same behaviour. But I cannot workout
- why the
when Whatever {}option is not entered (attempt #1) - what the
|cis doing - even though I can see that it works in the single subscript case (attempt #2)
I have done some experimenting with multi postcircumfix:<[ ]>( DataFrame:D $df, @slicer where Range|List ) is export {} but this seems to overcomplicate matters.
==================
Great answer from @jonathan building on the original from @Lizmat - thanks! Here is the final, working code:
class Series does Positional {
has Real @.data = [0.1,0.2,0.3];
method elems {
@!data.elems
}
method AT-POS( |p ) is raw {
@!data.AT-POS( |p )
}
}
class DataFrame does Positional {
has Series @.series;
method elems {
@!series.elems
}
method AT-POS( |p ) is raw {
@!series.AT-POS( |p )
}
}
my $df = DataFrame.new( series => Series.new xx 3 );
say $df[1].data; #[0.1 0.2 0.3]
say $df[1][2]; #0.3
say $df[0,1]; #(Series.new(data => $[0.1, 0.2, 0.3]) Series.new(data => $[0.1, 0.2, 0.3]))
say $df[1;2]; #0.3
say $df[1;*]; #(0.1 0.2 0.3)
say $df[*;1]; #(0.2 0.2 0.2)