On 2004/9/04, [EMAIL PROTECTED] (Larry Wall) wrote:
>Yow.  Presumably "nth" without an argument would mean the last.  So
>    @ints[1st..nth]
>means
>    @ints[*]

Yeah, I was thinking something like that.  And if the arg is an actual 
array, maybe it returns the max dimension(s)?  I think you'd get that 
anyway for one dimension...  in scalar context it would just be the same 
as using the array itself, since it returns the number of elements:

     @degree=('a'..'f');
     nth @degree == nth(@degree) == nth([EMAIL PROTECTED]) == nth(6) == 6th
     nth @degree == @degree.nth == 6

Hm, some ambiguity there, since 6 isn't quite the same as 6th (although 
of course it only makes sense for 6th to evaluate to 6 in numeric 
context).  So the ".nth" method needs to return an ordinal?

But with multiple dimensions, nth would return the max for each one:
     @degree is shape (5;6;7);
     nth @degree != nth ([EMAIL PROTECTED])
     nth @degree == @degree.nth == (5, 6, 7)  # or is that (5;6;7)?  
                                              # or (5th;6th;7th)?

So @degree[nth @degree] == @degree[nth; nth; nth];

Something like that....

>And if it's a unary with an optional arg, we can write
>
>    @ints[nth+1..nth-1]
>
>Hmm, that's not quite right.  My wife looks over my shoulder and 
>says "What about zth?"  Not knowing the history of A..Z already...

=)  Was that supposed to be first to last again, or second to second 
last?  1st, 2nd, nth could all take an arg; but I think no arg should 
mean the same as an argument of zero.  Then nth+1 = 1st+0 = 2nd-1, etc.

     @int[1st+0 .. nth-0]                 # first to last
     @int[1st+1 .. nth-1]                 # second to second last
     @int[1st+2 .. nth-2]                 # third to third last

     @int[1st+$offset .. nth-$offset]     #whateverth to whateverth last
     @int[nth(1+$offset)..nth(-$offset)]  #same thing

Using nth(1) to mean "first" looks better with the brackets, I guess.  
(Psychologically it reads more like "the nth position is 1" than "the 
nth position plus 1 more".)  There's still that problem of whether 0th 
should be at the beginning or the end (or both (or neither)).  Having 
both +0th and -0th is a nice symmetry, but it's more suited to humans 
than programmers; one reason for wanting to make -1 the last position is 
so that you can wrap around simply by decrementing your counter.

Whichever way we try to go, Murphy's Law predicts you'll always wish the 
zero were at the other end.  I think it makes more sense to start 
counting with the first==nth(1) and go up to the last=nth(0)=nth: 
because counting from the front is likely to happen more often than 
counting from the end; and because 1st(0) gives us a symmetrical 
counterpart to nth(0).


                            -David "nth degrees of exasperation" Green

Reply via email to