develooper Front page | perl.perl5.porters | Postings from January 2014

Re: Range idea

Thread Previous | Thread Next
From:
Aristotle Pagaltzis
Date:
January 23, 2014 19:33
Subject:
Re: Range idea
Message ID:
20140123193311.GE10247@fernweh.localdomain
* Paul "LeoNerd" Evans <leonerd@leonerd.org.uk> [2014-01-23 17:50]:
> What would this do?
>
>   my @range = 2 .. lastindex;
>   my @letters = ('A' .. 'Z');
>
>   say @letters[@range];
>
> ?
>
> Ultimately, 'lastindex' is going to fail however this works.

What Abigail said: I don’t see this as a problem any more than `next`
dying outside of loops.

> Why not just
>
>   sub slice
>   {
>      my ( $first, $last, @items ) = @_;
>      $last += @items if $last < 0;
>      return @items[$first..$last];
>   }
>
>   say slice 2, -1, 'A' .. 'Z'
>
> This justworks.

The pure-Perl implementation has a cost, as Merijn said.

It bothers me that indexing from the front of the list is 0-based but
indexing from the end of the list is 1-based. Maybe this is a case of
foolish consistency. If you did want to be consistent you’d have to be
able to distinguish +0 from -0… which doesn’t seem a sane interface for
entirely different reasons. But having a named placeholder such as the
lastindex() function I proposed sidesteps these issues to begin with.

(As an irrelevant-because-trivially-addressed nitpick, I find it weird
that you allow end-based indexing for the end of the slice but not for
the start. What if I want the two last elements of the list, no matter
how long it is?)

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About