On Fri, Jul 12, 2002 at 12:35:18PM -0400, darren chamberlain wrote:
> You could create first/last/etc methods in terms of splice:
> 'first' => sub {
> my $list = shift;
> my $howmany = shift || 1;
> _dotop({}, $list, 'splice', [ 0, $howmany ]);
> },
> 'last' => sub {
> my $list = shift;
> my $howmany = shift || 1;
> _dotop({}, $list, 'splice', [ -$howmany, $howmany ]);
> }
I just realised these are incompatible with the current versions
which just return a single item, the first or last in the list.
These new versions return a list of arguments. We would need to support
returning a single item in the case of no arguments passed, e.g.
[% list.first %] => x,
[% list.first(1) %] => [ x ]
[% list.first(3) %] => [ x, y, z ]
Also, you could call the splice() method direct rather than going
through _dotop(). Something like this:
e.g.
sub list_splice_vmeth { blah blah }
our $LIST_OPS = {
...etc...
splice => \&list_splice_vmeth,
first => sub {
my $list = shift;
return $list->[0] unless @_;
return list_splice_vmeth(0, @_);
},
last => ...etc...
A