Author: Benjamin Peterson <[email protected]>
Branch: py3k
Changeset: r53679:d242a858d444
Date: 2012-03-15 11:31 -0500
http://bitbucket.org/pypy/pypy/changeset/d242a858d444/
Log: completely move reduce to _functools
diff --git a/lib_pypy/_functools.py b/lib_pypy/_functools.py
--- a/lib_pypy/_functools.py
+++ b/lib_pypy/_functools.py
@@ -1,7 +1,27 @@
""" Supplies the internal functions for functools.py in the standard library
"""
-# reduce() has moved to _functools in Python 2.6+.
-reduce = reduce
+sentinel = object()
+
+def reduce(func, sequence, initial=sentinel):
+ """reduce(function, sequence[, initial]) -> value
+
+Apply a function of two arguments cumulatively to the items of a sequence,
+from left to right, so as to reduce the sequence to a single value.
+For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
+((((1+2)+3)+4)+5). If initial is present, it is placed before the items
+of the sequence in the calculation, and serves as a default when the
+sequence is empty."""
+ iterator = iter(sequence)
+ if initial is sentinel:
+ try:
+ initial = next(iterator)
+ except StopIteration:
+ raise TypeError("reduce() of empty sequence with no initial value")
+ result = initial
+ for item in iterator:
+ result = func(result, item)
+ return result
+
class partial(object):
"""
diff --git a/pypy/module/__builtin__/__init__.py
b/pypy/module/__builtin__/__init__.py
--- a/pypy/module/__builtin__/__init__.py
+++ b/pypy/module/__builtin__/__init__.py
@@ -20,7 +20,6 @@
'all' : 'app_functional.all',
'sum' : 'app_functional.sum',
'map' : 'app_functional.map',
- 'reduce' : 'app_functional.reduce',
'filter' : 'app_functional.filter',
'zip' : 'app_functional.zip',
'vars' : 'app_inspect.vars',
diff --git a/pypy/module/__builtin__/app_functional.py
b/pypy/module/__builtin__/app_functional.py
--- a/pypy/module/__builtin__/app_functional.py
+++ b/pypy/module/__builtin__/app_functional.py
@@ -93,28 +93,6 @@
else:
return result
-sentinel = object()
-
-def reduce(func, sequence, initial=sentinel):
- """reduce(function, sequence[, initial]) -> value
-
-Apply a function of two arguments cumulatively to the items of a sequence,
-from left to right, so as to reduce the sequence to a single value.
-For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
-((((1+2)+3)+4)+5). If initial is present, it is placed before the items
-of the sequence in the calculation, and serves as a default when the
-sequence is empty."""
- iterator = iter(sequence)
- if initial is sentinel:
- try:
- initial = next(iterator)
- except StopIteration:
- raise TypeError("reduce() of empty sequence with no initial value")
- result = initial
- for item in iterator:
- result = func(result, item)
- return result
-
def filter(func, seq):
"""filter(function or None, sequence) -> list, tuple, or string
diff --git a/pypy/module/__builtin__/test/test_functional.py
b/pypy/module/__builtin__/test/test_functional.py
--- a/pypy/module/__builtin__/test/test_functional.py
+++ b/pypy/module/__builtin__/test/test_functional.py
@@ -87,18 +87,6 @@
def test_three_lists(self):
assert zip([1,2,3], [1,2], [1,2,3]) == [(1,1,1), (2,2,2)]
-class AppTestReduce:
- def test_None(self):
- raises(TypeError, reduce, lambda x, y: x+y, [1,2,3], None)
-
- def test_sum(self):
- assert reduce(lambda x, y: x+y, [1,2,3,4], 0) == 10
- assert reduce(lambda x, y: x+y, [1,2,3,4]) == 10
-
- def test_minus(self):
- assert reduce(lambda x, y: x-y, [10, 2, 8]) == 0
- assert reduce(lambda x, y: x-y, [2, 8], 10) == 0
-
class AppTestFilter:
def test_None(self):
assert list(filter(None, ['a', 'b', 1, 0, None])) == ['a', 'b', 1]
diff --git a/pypy/module/__builtin__/test/test_reduce.py
b/pypy/module/__builtin__/test/test_reduce.py
deleted file mode 100644
--- a/pypy/module/__builtin__/test/test_reduce.py
+++ /dev/null
@@ -1,36 +0,0 @@
-import autopath
-
-
-class AppTestReduce:
- def test_None(self):
- raises(TypeError, reduce, lambda x, y: x+y, [1,2,3], None)
-
- def test_sum(self):
- assert reduce(lambda x, y: x+y, [1,2,3,4], 0) == 10
- assert reduce(lambda x, y: x+y, [1,2,3,4]) == 10
-
- def test_minus(self):
- assert reduce(lambda x, y: x-y, [10, 2, 8]) == 0
- assert reduce(lambda x, y: x-y, [2, 8], 10) == 0
-
- def test_from_cpython(self):
- class SequenceClass(object):
- def __init__(self, n):
- self.n = n
- def __getitem__(self, i):
- if 0 <= i < self.n:
- return i
- else:
- raise IndexError
-
- from operator import add
- assert reduce(add, SequenceClass(5)) == 10
- assert reduce(add, SequenceClass(5), 42) == 52
- raises(TypeError, reduce, add, SequenceClass(0))
- assert reduce(add, SequenceClass(0), 42) == 42
- assert reduce(add, SequenceClass(1)) == 0
- assert reduce(add, SequenceClass(1), 42) == 42
-
- d = {"one": 1, "two": 2, "three": 3}
- assert reduce(add, d) == "".join(d.keys())
-
diff --git a/pypy/module/__builtin__/test/test_range.py
b/pypy/module/test_lib_pypy/test__functools.py
rename from pypy/module/__builtin__/test/test_range.py
rename to pypy/module/test_lib_pypy/test__functools.py
--- a/pypy/module/__builtin__/test/test_range.py
+++ b/pypy/module/test_lib_pypy/test__functools.py
@@ -1,6 +1,6 @@
-import autopath
+from _functools import reduce
-class AppTestRange:
+class TestRange:
def test_range_toofew(self):
raises(TypeError, range)
@@ -22,7 +22,7 @@
def test_range_twoargs(self):
assert list(range(1, 2)) == [1]
-
+
def test_range_decreasingtwoargs(self):
assert list(range(3, 1)) == []
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit