1.1 --- a/lib/__builtins__/sequence.py Sat Dec 10 17:32:16 2016 +0100
1.2 +++ b/lib/__builtins__/sequence.py Sat Dec 10 17:35:58 2016 +0100
1.3 @@ -94,7 +94,10 @@
1.4 # start index.
1.5
1.6 if start is None:
1.7 - start = 0
1.8 + if step > 0:
1.9 + start = 0
1.10 + else:
1.11 + start = length - 1
1.12 else:
1.13 start = _get_absolute_index(start, length)
1.14
1.15 @@ -102,7 +105,10 @@
1.16 # otherwise normalising any end index.
1.17
1.18 if end is None:
1.19 - end = length
1.20 + if step > 0:
1.21 + end = length
1.22 + else:
1.23 + end = -1
1.24 else:
1.25 end = _get_absolute_index(end, length)
1.26
2.1 --- a/lib/__builtins__/span.py Sat Dec 10 17:32:16 2016 +0100
2.2 +++ b/lib/__builtins__/span.py Sat Dec 10 17:35:58 2016 +0100
2.3 @@ -21,15 +21,15 @@
2.4
2.5 from __builtins__.sequence import _max, _min
2.6
2.7 -class xrange:
2.8 +class slice:
2.9
2.10 - "Implementation of xrange."
2.11 + "Implementation of slice."
2.12
2.13 NO_END = object()
2.14
2.15 - def __init__(self, start_or_end, end=NO_END, step=1):
2.16 + def __init__(self, start_or_end=None, end=NO_END, step=1):
2.17
2.18 - "Initialise the xrange with the given 'start_or_end', 'end' and 'step'."
2.19 + "Initialise the slice with the given 'start_or_end', 'end' and 'step'."
2.20
2.21 if end is xrange.NO_END:
2.22 self.start = 0
2.23 @@ -38,8 +38,30 @@
2.24 self.start = start_or_end
2.25 self.end = end
2.26
2.27 + if step == 0:
2.28 + raise ValueError(self.step)
2.29 +
2.30 self.step = step
2.31
2.32 + def __str__(self):
2.33 +
2.34 + "Return a string representation."
2.35 +
2.36 + b = buffer([self.__name__, "(", self.start, ", ", self.end, ", ", self.step, ")"])
2.37 + return str(b)
2.38 +
2.39 + __repr__ = __str__
2.40 +
2.41 +class xrange(slice):
2.42 +
2.43 + "Implementation of xrange."
2.44 +
2.45 + def __init__(self, start_or_end, end=slice.NO_END, step=1):
2.46 +
2.47 + "Initialise the xrange with the given 'start_or_end', 'end' and 'step'."
2.48 +
2.49 + get_using(slice.__init__, self)(start_or_end, end, step)
2.50 +
2.51 # Constrain the end according to the start and step.
2.52
2.53 if step > 0:
2.54 @@ -51,15 +73,6 @@
2.55
2.56 self.current = self.start
2.57
2.58 - def __str__(self):
2.59 -
2.60 - "Return a string representation."
2.61 -
2.62 - b = buffer([self.__name__, "(", self.start, ", ", self.end, ", ", self.step, ")"])
2.63 - return str(b)
2.64 -
2.65 - __repr__ = __str__
2.66 -
2.67 def __len__(self):
2.68
2.69 "Return the length of the range."
2.70 @@ -83,16 +96,6 @@
2.71 self.current += self.step
2.72 return current
2.73
2.74 -class slice(xrange):
2.75 -
2.76 - "Implementation of slice."
2.77 -
2.78 - def __init__(self, start_or_end=None, end=xrange.NO_END, step=1):
2.79 -
2.80 - "Initialise the slice with the given 'start_or_end', 'end' and 'step'."
2.81 -
2.82 - get_using(xrange.__init__, self)(start_or_end, end, step)
2.83 -
2.84 def range(start_or_end, end=None, step=1):
2.85
2.86 "Implementation of range."
3.1 --- a/tests/slice.py Sat Dec 10 17:32:16 2016 +0100
3.2 +++ b/tests/slice.py Sat Dec 10 17:35:58 2016 +0100
3.3 @@ -10,3 +10,4 @@
3.4 print l[5:2:-1] # [5, 4, 3]
3.5 print l[1:9:2] # [1, 3, 5, 7]
3.6 print l[9:1:-2] # [9, 7, 5, 3]
3.7 +print l[::-1] # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]