What's the equivalent in Perl 6 to star expressions in Python?
Perl5:
sub drop_first_last { avg( @_[ 1 .. $#_-1 ] ) } #this
sub drop_first_last { shift;pop;avg@_ } #or this
Perl6:
sub drop_first_last { avg( @_[ 1 .. @_.end-1 ] ) }
sub drop_first_last(Seq() \seq, $n = 1) { seq.skip($n).head(*-$n) };
say drop_first_last( 1..10 ); # (2 3 4 5 6 7 8 9)
say drop_first_last( 1..10, 2 ); # (3 4 5 6 7 8)
The way it works: convert whatever the first argument is to a Seq
, then skip $n
elements, and then keep all except the last $n
elements.
Use a slice.
sub drop_first_last (@grades) {
return avg(@grades[1..*-2])
}