I just thought, writing a universal slice function won't be hard - and here is what i sketched:
function arraySlice(arr, start=invalid, finish=invalid, step_=1):
if step_ = 0 then print "ValueError: slice step cannot be zero" : STOP
if start = invalid then if step_ > 0 then start = 0 else start = arr.count() - 1
if finish = invalid then if step_ > 0 then finish = arr.count() - 1 else finish = 0
if start < 0 then start = arr.count() + start 'negative counts backwards from the end
if finish < 0 then finish = arr.count() + finish
res = [ ]
for i = start to finish step step_:
res.push(arr[i])
end for
return res
end function
And here is testing it to act like the python examples above:
BrightScript Debugger> a = [00,11,22,33,44]
BrightScript Debugger> ? arraySlice(a, 2, 3)
22
33
BrightScript Debugger> ? arraySlice(a, 3)
33
44
BrightScript Debugger> ? arraySlice(a, invalid, -3)
0
11
22
BrightScript Debugger> ? arraySlice(a, invalid, invalid, -2)
44
22
0
Just like in Python it can slice forward - or backwards (-1 step reverses the slice) - or from the beginning to certain point - or from a position till the end - or every N-th element (when step is N>1). One difference is that it's inclusive of the slice end, it's easier for beginners that way (and was easier for me to write too).