slice negative counter

Notices and updates
Locked
hs
Posts: 13
Joined: Sun Sep 03, 2006 8:32 am

slice negative counter

Post by hs »

Hi all!

it's probably been discussed before but i haven't searched enough (i got the result on negative offset for slice)

is there any easy way to get negative counter act as position?
inclusive start and end
like:
(set 'a '(1 2 3 4 5 6 7))

(2 -2 a) ;want (3 4 5 6), newlisp gives (3 4 5 6 7)

in code:
(define (my-slice l s c)
(if (>= c 0)
(slice l s c)
(slice l s (+ (length l) c -1))))

a; (1 2 3 4 5 6 7)
(my-slice a 2 -2) ;gives (3 4 5 6)
(my-slice a 2 0); gives ()
(my-slice a 2 2); gives (3 4)

it would be nice to have it in implicit slicing too like (2 -2 a)
thx!

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

looks like you can now do this!
9.1.11 release notes
negative second length in slicing now interpreted as offset
from the right: (2 3 "abcdefg") => "cde", (2 -3 "abcdefg") => "cd"
before negative length would be interpreted as going to the end

hs
Posts: 13
Joined: Sun Sep 03, 2006 8:32 am

Post by hs »

cormullion wrote:looks like you can now do this!
9.1.11 release notes
negative second length in slicing now interpreted as offset
from the right: (2 3 "abcdefg") => "cde", (2 -3 "abcdefg") => "cd"
before negative length would be interpreted as going to the end
it's inclusive in left endpoint and non-inclusive at the right endpoint, similar to python

>>> a="abcdefg"
>>> a[2:-3]
'cd'
>>> a[2:]
'cdefg'
>>> a[2:0]
''

thanks Lutz!

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

nobody confuse (a 2 0) -> "c" and (2 0 a) -> "", in newLISP the indices are before the string/list when slicing.

Lutz

frontera000
Posts: 72
Joined: Sun Jun 11, 2006 8:02 pm
Location: berkeley, california
Contact:

v9.1.11 slice

Post by frontera000 »

> (slice '(a b c d e f) 2 -1)
(c d e)
> (slice '(a b c d e f) 2 )
(c d e f)

Second one seems to be correct.

Both should give same results, no?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Since v 9.1.11, both are correct now. Now a negative length spec in slice is taken as offset from the end. The manual in 9.1.11 hasn't been updated yet and still shows the old way of treating a negative parameter.

- glad to see you on this board again ;-)

Lutz

frontera000
Posts: 72
Joined: Sun Jun 11, 2006 8:02 pm
Location: berkeley, california
Contact:

Post by frontera000 »

Thanks. I was trapped in Java hell for a year. :)

Locked