Possible improvement to slice opperations.

Ron Adam rrr at ronadam.com
Mon Sep 5 18:33:33 EDT 2005


Bengt Richter wrote:

> On Mon, 5 Sep 2005 18:09:51 +0200, "Fredrik Lundh" <fredrik at pythonware.com> wrote:


> OTOH, ISTM we must be careful not to label an alternate alpha-version
> "way to model the real world" as a "misunderstanding" just because it is alpha,
> and bugs are apparent ;-)

Thanks! I couldn't have said this myself.  :-)


> BTW, it just occurred to me that alternate slice member semantics could be RELATIVE,
> EACH depending on sign. I.e.,
> 
>     x[start:stop:step]
> 
> could mean start with x[start] as now as a starting point reference point element,
> then let stop be a relative range length in either direction from the
> starting point, and step absolute value indicating step size, and its sign
> indicating insertion side for zero length slices. In effect, stop would
> be a count for the slice range in either direction starting with the starting
> element
> 
>     s = 'abcde'
>     s[2:2]     => 'cd'
>     s[2:-2]    => 'cb'
>     s[-2:-3]   => 'dcb'
>     s[-2:0]    => ''
>     s[2:0]     => ''
>     s[-2:-3:2] => 'db'
>     r = range(10)
>     r[5:0] = 'a'
>     r => [0, 1, 2, 3, 4, 5, 'a', 6, 7, 8, 9]
>     r[-2:0:-1] = 'b'
>     r => [0, 1, 2, 3, 4, 5, 'a', 6, 7, 'b', 8, 9]
>     r[-2:0] = ['c', 'd']
>     r => [0, 1, 2, 3, 4, 5, 'a', 6, 7, 'b', 8, c, d, 9]

Interesting, so it would be...

    slice( position, count, step )

Items are addressed directly so there's no 'gap's to account for.


> note that slice assignment would work by iterating through the right hand
> sequence, which could do interesting things:
> 
>     r = range(6)
>     r[3:-2] = 'ab'
>     r => [0, 1, 'b', 'a', 4, 5]
> but two reverse relative slices match order, so
>     r = range(10)
>     r[5:-3] = range(10)[-1:-3]  # rh seq is 9, 8
>     r => [0, 1, 8, 9, 4, 5]
> 
> I think this is kind of interesting, and I probably wouldn't have thought of
> it if I had not been for Ron's enthusiasm for his "misunderstanding" ;-)
> 
> In a way, "misunderstandings" are the mutations of open source evolution of ideas,
> most of which die, but some of which mutate again and may occasionally survive.
> So we need them ;-)


Here's another possible "misunderstanding".

     (or alternative alpha-version)  ;-)


Have you thought of using a slice object as a buffer pointer for 
indirect operations?

     r = range(100)
     d = [10:20]
     r.range_add(d,5)   # Adds 5 to items in range d

     d = d[5:]    -> [15:20]   # relative range modification.
                               # slice of a slice object

     r.range_sub(d,3)   # subtract 3 from items in range d

Or more flexible ...
     r.range_modify(d, add(), 5)

Using your suggestion that would be...

    r = range(100)
    d = [10:10]
    r.range_add(d,5)

    d = d[5:]   -> [15:5]  # interesting symmetry.
    r.range_sub(d,3)

Of course adding and subtracting slice objects could also be possible.

    d = [10:20]
    e = [15:25]
    f = d + e    ->  [10:25]

or ...

    d = [10:10]
    e = [15:10]
    f = d + e    ->  [10:15]


Cheers,
Ron


> Regards,
> Bengt Richter



More information about the Python-list mailing list