Re: Negative array subscripts
HaloO, David Green wrote: Also, this would solve a problem I've been wondering about with "funny" shapes being lexically scoped. [..] However, what if you pass the funny array along with a (funny) index? E.g. our @flavours (1..32); my $favourite = get_fave(@flavours); #returns index of selected flavour warn "Sorry, all out of @flavours[$favourite]!" unless in_stock(@flavours, $favourite); Which means that you'd be checking for the wrong thing to be in stock. (Actually, you'd also get the wrong index back from get_fave(), so in this limited example, the two errors would probably cancel each other out, but the warning message will print the wrong flavour because it's using the shifted index.) It's interesting to get the signature for this &get_fave routine. As stated it should be :(@a --> Int where {$_ ~~ @[EMAIL PROTECTED]). The implementation of &get_favo has to be generic with respect to the array shape otherwise a type error occurs when e.g. the shape is (-5..-1) and the body of &get_favo is coded in terms of (0..4). And I wonder how this generic index handling can be done by means of * dwimmery along the lines of sub get_favo (@a --> Int where {$_ ~~ @[EMAIL PROTECTED]) { my $idx = *+0; # how is this connected to @a? while check_favo(@a[$idx]) and $idx < *-0 # same as @a.end? { $idx++; } return $idx; } BTW, is the where clause in the signature still seeing the outside shape? Or does one have to say something like @a.OUTER::begin to get at it because the @a of the sub always has got shape ([EMAIL PROTECTED])? Regards, TSa. --
Re: Negative array subscripts
David Green wrote: On 2/5/07, David Green wrote: Then we wouldn't need * to count backwards, although it's still useful to allow us to count past the end of an array. There are all sorts of variations on this scheme, such as whether * is the last element or the one after that, etc., or whether 0 should be the first element or the last, and so on. In some ways, I like not having a [0] index at all: programmers may be used to counting from zero, but normal humans start with first, second, third, ... third last, second last,... My feelings are Perl 6 should stick to 0 being the index of the first element of a list. Otherwise we might alienate programmers from P5 and nearly every other language. Couldn't the first array index be adjusted by adding a user defined Parrot grammar definition that applies the transformation +1 inside [] operators instead; maybe this could be accessible via a Perl "use" pragma.
Re: Negative array subscripts
Blair Sutton writes: > David Green wrote: > > > In some ways, I like not having a [0] index at all: programmers may > > be used to counting from zero, but normal humans start with first, > > second, third, ... third last, second last,... > > My feelings are Perl 6 should stick to 0 being the index of the first > element of a list. Otherwise we might alienate programmers from P5 and > nearly every other language. Couldn't the first array index be > adjusted by adding a user defined Parrot grammar definition that > applies the transformation +1 inside [] operators instead; maybe this > could be accessible via a Perl "use" pragma. Hmmm, a pragma's a bit heavyweight for this; how about being able to set this with a special global variable -- that sure sounds handy ... Smylers