enhancing slicing

Steve Holden sholden at holdenweb.com
Thu Aug 23 19:00:34 EDT 2001


"Bengt Richter" <bokr at accessone.com> wrote in message
news:3b856114.272278696 at wa.news.verio.net...
> On Thu, 23 Aug 2001 14:24:40 +0200, "Juan Valiño" <juanv at posta.unizar.es>
wrote:
>
> >Many times I want to extract a sublist and I have the initial position  p
> >plus the length  n.
> >So I do:
> >v[p:p+n]
> >
> >Many times the initial position  p  is an expression. Hence, I have to
> >repeat it tediously (and with a possible lost of performance) or assign
it
> >previously to a variable. The last is boring and is a little trick out of
> >the logic of the program
> >
> >My proposal is to introduce a new operator, for instance # (count):
> >v[5 # 2]
> >Extract 2 elements starting at 5: [ v[5], v[6] ]. Equivalent to v[5:5+2]
> >
> >v[5 # -2]
> >Extract 2 elements ending at (but not including the) 5: [ v[3], v[4] ].
> >Equivalent to v[5-2:5]
> >
> >I think that this little syntactic sugar allows programs more compact and
> >readable. Also, I had less "start and end point paranoias". I suppose
that
> >the implementation is easy and the current programs are not affected at
all.
> >
> '#' is a comment delimiter which would make that usage awful. But I think
>    v[5:+=2]
> could be possible and also
>    v[-=2:5]
>
> I.e.,
>    v[a:+=b]
> and
>    v[-=b:a]
> would be equivalent to
>    v[a:a+b]
> and
>    v[a-b:a]
> IOW,
>    op=slice_parameter
> implies
>    slice_parameter_value_to_use = the_other_slice_parameter op
slice_parameter
>
> This might have interesting (maybe even useful ;-) consequences
> for ops other than + and - and might not be that bad to implement, since
> op= tokens are distinct.
>
If I can remember my Icon correctly its slicing mechanism recognised signs
as significant, so

    seq[a:+b]

meant take a slice of length b starting at position a, whereas

    seq[a:b]

took a slice from positions a to b. Of course, strictly speaking this could
induce code breakage in Python -- although the plus sign is redundant in
such existing code, it might occur. Shame the issue wasn't addressed when
slices were introduced, though.

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list