From a20ff9cf5d1e37c379700e9a9df30b55cc69ce12 2013-03-28 20:26:04
From: MinRK <benjaminrk@gmail.com>
Date: 2013-03-28 20:26:04
Subject: [PATCH] don't create lists

This is crazy, and makes getting all partitions of a sequence an n^2 operation
---

diff --git a/IPython/parallel/client/map.py b/IPython/parallel/client/map.py
index dab382a..f9ee3a3 100644
--- a/IPython/parallel/client/map.py
+++ b/IPython/parallel/client/map.py
@@ -56,9 +56,9 @@ else:
     arrayModules.append({'module':numarray,
         'type':numarray.numarraycore.NumArray})
 
-class Map:
+class Map(object):
     """A class for partitioning a sequence using a map."""
-            
+    
     def getPartition(self, seq, p, q):
         """Returns the pth partition of q partitions of seq."""
         
@@ -66,25 +66,24 @@ class Map:
         if p<0 or p>=q:
           print "No partition exists."
           return
-          
-        remainder = len(seq)%q
-        basesize = len(seq)//q
-        hi = []
-        lo = []
-        for n in range(q):
-            if n < remainder:
-                lo.append(n * (basesize + 1))
-                hi.append(lo[-1] + basesize + 1)
-            else:
-                lo.append(n*basesize + remainder)
-                hi.append(lo[-1] + basesize)
+        
+        N = len(seq)
+        remainder = N % q
+        basesize = N // q
+        
+        if p < remainder:
+            low = p * (basesize + 1)
+            high = low + basesize + 1
+        else:
+            low = p * basesize + remainder
+            high = low + basesize
         
         try:
-            result = seq[lo[p]:hi[p]]
+            result = seq[low:high]
         except TypeError:
             # some objects (iterators) can't be sliced,
             # use islice:
-            result = list(islice(seq, lo[p], hi[p]))
+            result = list(islice(seq, low, high))
             
         return result