Revision
3449
Author
ernest.prabhakar@gmail.com
Date
2010-02-08 15:04:34 -0800 (Mon, 08 Feb 2010)

Log Message

Initial mapreduce specs

Modified Paths

Diff

Modified: MacRuby/trunk/lib/dispatch/enumerable.rb (3448 => 3449)


--- MacRuby/trunk/lib/dispatch/enumerable.rb	2010-02-08 23:04:22 UTC (rev 3448)
+++ MacRuby/trunk/lib/dispatch/enumerable.rb	2010-02-08 23:04:34 UTC (rev 3449)
@@ -30,11 +30,11 @@
   end
 
   # Parallel +collect+ plus +inject+
-  # Accumulates in +result+ via +op+ (default = '<<')
-  # Only works if result doesn't depend on the order elements are processed.
-  def p_mapreduce(result=[], op=:<<, &block)
-    raise ArgumentError if not result.respond_to? :op
-    p_result = Dispatch.wrap(result)
+  # Accumulates in +initial+ via +op+ (default = '<<')
+  def p_mapreduce(initial, op=:<<, &block)
+    raise ArgumentError if not initial.respond_to? :op
+    # Since exceptions from a Dispatch block act funky 
+    p_result = Dispatch.wrap(initial)
     self.p_each_with_index do |obj, i|
      val = block.call(obj)
      p_result.send(op, val)

Modified: MacRuby/trunk/spec/macruby/library/dispatch/enumerable_spec.rb (3448 => 3449)


--- MacRuby/trunk/spec/macruby/library/dispatch/enumerable_spec.rb	2010-02-08 23:04:22 UTC (rev 3448)
+++ MacRuby/trunk/spec/macruby/library/dispatch/enumerable_spec.rb	2010-02-08 23:04:34 UTC (rev 3449)
@@ -12,7 +12,7 @@
         @ary.respond_to?(:p_each).should == true
       end
 
-      it "behaves like each" do
+      it "should behave like each" do
         @sum1 = 0
         @ary.each {|v| @sum1 += v*v}
         @sum2 = 0
@@ -31,7 +31,7 @@
         @ary.respond_to?(:p_each).should == true
       end
       
-      it "behaves like each_with_index" do
+      it "should behave like each_with_index" do
         @sum1 = 0
         @ary.each_with_index {|v, i| @sum1 += v**i}
         @sum2 = 0
@@ -47,15 +47,47 @@
     
     describe :p_map do
       it "exists on objects that support Enumerable" do
-        @ary.respond_to?(:p_each).should == true
+        @ary.respond_to?(:p_map).should == true
       end
-      it "behaves like map" do
-        @ary.should.respond_to? :p_map
+      it "should behave like map" do
+        map1 = @ary.map {|v| v*v}
+        map2 = @ary.p_map {|v| v*v}
+        map1.should == map2
       end
       
       it "executes concurrently" do
         true.should == true
       end
     end
+
+    describe :p_mapreduce do
+      it "exists on objects that support Enumerable" do
+        @ary.respond_to?(:p_mapreduce).should == true
+      end
+      
+      it "should behave like an unordered map" do
+        map1 = @ary.map {|v| v*v}
+        map2 = @ary.p_mapreduce([]) {|v| v*v}
+        map1.should == map2.sort
+      end
+
+      it "should use any result that takes :<< " do
+        map1 = @ary.map {|v| "%x" % 10+v}
+        map2 = @ary.p_mapreduce("") {|v| "%x" % 10+v}   
+        map1.each do |s|
+          map2.index(s).should_not == nil
+        end
+      end
+
+      it "should allow custom accumulator methods" do
+        map2 = @ary.p_mapreduce(0, :|) {|v| v**2}   
+        map2.should == (2 | 4 | 8)
+      end
+      
+      it "executes concurrently" do
+        true.should == true
+      end
+    end
+
   end
 end
\ No newline at end of file