1.1 --- a/lib/__builtins__/__init__.py Thu Dec 01 21:23:11 2016 +0100
1.2 +++ b/lib/__builtins__/__init__.py Thu Dec 01 23:06:26 2016 +0100
1.3 @@ -75,7 +75,7 @@
1.4 from __builtins__.file import file
1.5 from __builtins__.float import float
1.6 from __builtins__.int import int
1.7 -from __builtins__.span import xrange, slice
1.8 +from __builtins__.span import range, slice, xrange
1.9 from __builtins__.iterator import itemiterator
1.10 from __builtins__.list import list
1.11 from __builtins__.long import long
1.12 @@ -92,7 +92,7 @@
1.13 from __builtins__.comparable import cmp, hash
1.14 from __builtins__.identity import callable, help, id, isinstance, issubclass, repr
1.15 from __builtins__.io import open, raw_input, print_, sysfile
1.16 -from __builtins__.iterable import all, any, enumerate, filter, iter, len, map, max, min, range, reduce, reversed, sorted, sum, zip
1.17 +from __builtins__.iterable import all, any, enumerate, filter, iter, len, map, max, min, reduce, reversed, sorted, sum, zip
1.18 from __builtins__.namespace import dir, globals, locals, vars
1.19 from __builtins__.numeric import abs, divmod, pow, round
1.20
2.1 --- a/lib/__builtins__/iterable.py Thu Dec 01 21:23:11 2016 +0100
2.2 +++ b/lib/__builtins__/iterable.py Thu Dec 01 23:06:26 2016 +0100
2.3 @@ -3,7 +3,7 @@
2.4 """
2.5 Iteration-related functions.
2.6
2.7 -Copyright (C) 2015 Paul Boddie <paul@boddie.org.uk>
2.8 +Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk>
2.9
2.10 This program is free software; you can redistribute it and/or modify it under
2.11 the terms of the GNU General Public License as published by the Free Software
2.12 @@ -19,8 +19,6 @@
2.13 this program. If not, see <http://www.gnu.org/licenses/>.
2.14 """
2.15
2.16 -from __builtins__.span import xrange
2.17 -
2.18 def all(iterable): pass
2.19 def any(iterable): pass
2.20 def enumerate(iterable): pass
2.21 @@ -59,12 +57,6 @@
2.22 lowest = arg
2.23 return lowest
2.24
2.25 -def range(start_or_end, end=None, step=1):
2.26 -
2.27 - "Implementation of range."
2.28 -
2.29 - return list(xrange(start_or_end, end, step))
2.30 -
2.31 def reduce(function, sequence, initial=None): pass
2.32 def reversed(sequence): pass
2.33 def sorted(iterable, cmp=None, key=None, reverse=False): pass
3.1 --- a/lib/__builtins__/span.py Thu Dec 01 21:23:11 2016 +0100
3.2 +++ b/lib/__builtins__/span.py Thu Dec 01 23:06:26 2016 +0100
3.3 @@ -3,7 +3,7 @@
3.4 """
3.5 Span-related objects.
3.6
3.7 -Copyright (C) 2015 Paul Boddie <paul@boddie.org.uk>
3.8 +Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk>
3.9
3.10 This program is free software; you can redistribute it and/or modify it under
3.11 the terms of the GNU General Public License as published by the Free Software
3.12 @@ -19,7 +19,9 @@
3.13 this program. If not, see <http://www.gnu.org/licenses/>.
3.14 """
3.15
3.16 -class xrange(object):
3.17 +from __builtins__.sequence import _max, _min
3.18 +
3.19 +class xrange:
3.20
3.21 "Implementation of xrange."
3.22
3.23 @@ -37,8 +39,32 @@
3.24 self.end = end
3.25
3.26 self.step = step
3.27 +
3.28 + # Constrain the end according to the start and step.
3.29 +
3.30 + if step > 0:
3.31 + self.end = _max(self.start, self.end)
3.32 + elif step < 0:
3.33 + self.end = _min(self.start, self.end)
3.34 + else:
3.35 + raise ValueError(self.step)
3.36 +
3.37 self.current = self.start
3.38 - self.limited = self.end is not xrange.NO_END
3.39 +
3.40 + def __str__(self):
3.41 +
3.42 + "Return a string representation."
3.43 +
3.44 + b = buffer([self.__name__, "(", self.start, ", ", self.end, ", ", self.step, ")"])
3.45 + return str(b)
3.46 +
3.47 + __repr__ = __str__
3.48 +
3.49 + def __len__(self):
3.50 +
3.51 + "Return the length of the range."
3.52 +
3.53 + return (self.end - self.start) / self.step
3.54
3.55 def __iter__(self):
3.56
3.57 @@ -50,9 +76,8 @@
3.58
3.59 "Return the next item or raise a StopIteration exception."
3.60
3.61 - if self.limited:
3.62 - if self.step < 0 and self.current <= self.end or self.step > 0 and self.current >= self.end:
3.63 - raise StopIteration()
3.64 + if self.step < 0 and self.current <= self.end or self.step > 0 and self.current >= self.end:
3.65 + raise StopIteration()
3.66
3.67 current = self.current
3.68 self.current += self.step
3.69 @@ -68,4 +93,10 @@
3.70
3.71 get_using(xrange.__init__, self)(start_or_end, end, step)
3.72
3.73 +def range(start_or_end, end=None, step=1):
3.74 +
3.75 + "Implementation of range."
3.76 +
3.77 + return list(xrange(start_or_end, end, step))
3.78 +
3.79 # vim: tabstop=4 expandtab shiftwidth=4
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/tests/range.py Thu Dec 01 23:06:26 2016 +0100
4.3 @@ -0,0 +1,18 @@
4.4 +l = range(0, 10)
4.5 +print l # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
4.6 +print len(l) # 10
4.7 +
4.8 +x = xrange(0, -10, -2)
4.9 +print x # __builtins__.span.xrange(0, -10, -2)
4.10 +print len(x) # 5
4.11 +
4.12 +for i in x:
4.13 + print i # 0
4.14 + # -2
4.15 + # -4
4.16 + # -6
4.17 + # -8
4.18 +
4.19 +x = xrange(0, -10, 2)
4.20 +print x # __builtins__.span.xrange(0, 0, 2)
4.21 +print len(x) # 0