[pypy-svn] rev 566 - in pypy/trunk/src/pypy/module: . test

tomek at codespeak.net tomek at codespeak.net
Tue May 27 15:50:30 CEST 2003


Author: tomek
Date: Tue May 27 15:50:30 2003
New Revision: 566

Added:
   pypy/trunk/src/pypy/module/test/test_funtional.py
Modified:
   pypy/trunk/src/pypy/module/builtin_app.py
Log:
small changes


Modified: pypy/trunk/src/pypy/module/builtin_app.py
==============================================================================
--- pypy/trunk/src/pypy/module/builtin_app.py	(original)
+++ pypy/trunk/src/pypy/module/builtin_app.py	Tue May 27 15:50:30 2003
@@ -5,7 +5,10 @@
 def map(function, *collections):
     if len(collections) == 1:
        #it's the most common case, so make it faster
-       return [function(x) for x in collections[0]]
+       if function is None:
+          return collections[0]
+       else:
+          return [function(x) for x in collections[0]]
     else:
        res = []
        idx = 0   
@@ -20,7 +23,10 @@
                  elem = None
               args.append(elem)
           if cont:
-              res.append(function(*args))
+              if function is None:
+                 res.append(tuple(args))
+              else:
+                 res.append(function(*args))
           else:
               return res
           idx = idx + 1
@@ -60,15 +66,16 @@
     return res
 
 
-def reduce(function, list, initial = None):
-    if initial is None:
+def reduce(function, list, *initialt):
+    if initialt:
+       initial, = initialt
+       idx = 0
+    else:
        try:
           initial = list[0]
        except IndexError:
           raise TypeError, "reduce() of empty sequence with no initial value"
        idx = 1
-    else:
-       idx = 0
     while 1:
        try:
          initial = function(initial, list[idx])

Added: pypy/trunk/src/pypy/module/test/test_funtional.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/module/test/test_funtional.py	Tue May 27 15:50:30 2003
@@ -0,0 +1,43 @@
+import testsupport
+from pypy.module.builtin_app import map, filter, reduce, zip
+
+class TestMap(testsupport.TestCase):
+
+   def test_map_identity1(self):
+      a = ['1', 2, 3, 'b', None]
+      b = a[:]
+      self.assertEqual(map(lambda x: x, a), a)
+      self.assertEqual(a, b)
+ 
+   def test_map_None1(self):
+      a = ['1', 2, 3, 'b', None]
+      b = a[:]
+      self.assertEqual(map(None, a), a)
+      self.assertEqual(a, b)
+
+   def test_map_badoperation(self):
+      a = ['1', 2, 3, 'b', None]
+      self.assertRaises(TypeError, map, lambda x: x+1, a)
+
+   def test_map_multiply_identity(self):
+      a = ['1', 2, 3, 'b', None]
+      b = [ 2, 3, 4, 5, 6]
+      self.assertEqual(map(None, a, b), [('1', 2), (2, 3), (3, 4), ('b', 5), (None, 6)])
+
+   def test_map_multiply(self):
+      a = [1, 2, 3, 4]
+      b = [0, 1, 1, 1]
+      self.assertEqual(map(lambda x, y: x+y, a, b), [1, 2, 4, 5])
+
+   def test_map_multiply(self):
+      a = [1, 2, 3, 4, 5]
+      b = []
+      self.assertEqual(map(lambda x, y: x, a, b), a)
+
+class TestZip(testsupport.TestCase):
+   pass
+      
+if __name__ == '__main__':
+    testsupport.main()
+
+


More information about the Pypy-commit mailing list