def merge_sort(unsorted): global app_count print "sorting %d unsorted files" % (len(unsorted)) # Sort all the individual files sorted = [sort(f) for f in unsorted] app_count += len(sorted) return treereduce(merge, sorted) # Note: could also code as below:. # Merge them all hierarchically """
def testSum(self): import random nums, boundnums = red.genlist() tot = sum(nums) print "Total of %s is %d" % (repr(nums), tot) treeres = treereduce(red.add, boundnums).get() print "treereduce(add, bound).get() = %d" % treeres dynres = dynreduce(red.add, boundnums).get() print "dynreduce(add, bound).get() = %d" % dynres self.assertEqual(treeres, tot) self.assertEqual(dynres, tot)
def testAssociative(self): """ Test reductions which are associative """ import itertools str = ["aaa", "bb", "c", "dd", "e", "fff"] bound = map(String, str) exp = ''.join(str) @func((String),(String, String)) def concat(a, b): return a + b foldres = foldl(concat, String(""), bound).get() treeres = treereduce(concat, bound).get() self.assertEquals(exp, foldres) self.assertEquals(exp, treeres)
def testAssociative(self): """ Test reductions which are associative """ import itertools str = ["aaa", "bb", "c", "dd", "e", "fff"] bound = map(String, str) exp = ''.join(str) @func((String), (String, String)) def concat(a, b): return a + b foldres = foldl(concat, String(""), bound).get() treeres = treereduce(concat, bound).get() self.assertEquals(exp, foldres) self.assertEquals(exp, treeres)