Page 1 of 1

slice negative counter

Posted: Wed Jul 25, 2007 11:17 am
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!

Posted: Fri Aug 03, 2007 5:44 pm
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

Posted: Sun Aug 05, 2007 9:09 am
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!

Posted: Sun Aug 05, 2007 11:00 am
by Lutz
nobody confuse (a 2 0) -> "c" and (2 0 a) -> "", in newLISP the indices are before the string/list when slicing.

Lutz

v9.1.11 slice

Posted: Mon Aug 06, 2007 6:04 am
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?

Posted: Mon Aug 06, 2007 12:32 pm
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

Posted: Mon Aug 06, 2007 4:22 pm
by frontera000
Thanks. I was trapped in Java hell for a year. :)