Revision: 3448 http://trac.macosforge.org/projects/ruby/changeset/3448 Author: ernest.prabhakar@gmail.com Date: 2010-02-08 15:04:22 -0800 (Mon, 08 Feb 2010) Log Message: ----------- Preliminary Dispatch mapreduce Modified Paths: -------------- MacRuby/trunk/lib/dispatch/enumerable.rb MacRuby/trunk/spec/macruby/library/dispatch/enumerable_spec.rb Modified: MacRuby/trunk/lib/dispatch/enumerable.rb =================================================================== --- MacRuby/trunk/lib/dispatch/enumerable.rb 2010-02-08 23:04:12 UTC (rev 3447) +++ MacRuby/trunk/lib/dispatch/enumerable.rb 2010-02-08 23:04:22 UTC (rev 3448) @@ -19,43 +19,48 @@ grp.wait end - # Parallel +inject+ - # Requires initial value since 'first' no longer special. - # Only works if result doesn't depend on the order elements are processed. - def p_inject(initial, &block) - @result = Dispatch.wrap(initial) - self.p_each { |obj| block.call(@result, obj) } - @result._done - return @result - end - # Parallel +collect+ + # Results match the order of the original array def p_map(&block) result = Dispatch.wrap(Array) self.p_each_with_index do |obj, i| result[i] = block.call(obj) end - result + result._done_ 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) + self.p_each_with_index do |obj, i| + val = block.call(obj) + p_result.send(op, val) + end + p_result + ._done_ + end + + # Parallel +select+; will return array of objects for which # +&block+ returns true. - # Useful if the test block is very expensive to run def p_find_all(&block) - @found_all = Dispatch.wrap(Array) - self.p_each { |obj| @found_all << obj if block.call(obj) } - @found_all._done # will this leak? + found_all = Dispatch.wrap(Array) + self.p_each { |obj| found_all << obj if block.call(obj) } + found_all._done # will this leak? end # Parallel +detect+; will return -one- match for +&block+ # but it may not be the 'first' - # Useful if the test block is very expensive to run + # Only useful if the test block is very expensive to run def p_find(&block) - @found = Dispatch.wrap(nil) + found = Dispatch.wrap(nil) self.p_each do |obj| - found = @found.nil? ? block.call(obj) : nil - @found = obj if found and @found.nil? + found = found.nil? ? block.call(obj) : nil + found = obj if found and found.nil? end - @found._done # will this leak? + found._done # will this leak? end end Modified: MacRuby/trunk/spec/macruby/library/dispatch/enumerable_spec.rb =================================================================== --- MacRuby/trunk/spec/macruby/library/dispatch/enumerable_spec.rb 2010-02-08 23:04:12 UTC (rev 3447) +++ MacRuby/trunk/spec/macruby/library/dispatch/enumerable_spec.rb 2010-02-08 23:04:22 UTC (rev 3448) @@ -30,8 +30,14 @@ it "exists on objects that support Enumerable" do @ary.respond_to?(:p_each).should == true end + it "behaves like each_with_index" do - @ary.should.respond_to? :p_each_with_index + @sum1 = 0 + @ary.each_with_index {|v, i| @sum1 += v**i} + @sum2 = 0 + @q = Dispatch.queue_for(@sum2) + @ary.p_each_with_index {|v, i| temp = v**i; @q.sync {@sum2 += temp} } + @sum1.should == @sum2 end it "executes concurrently" do @@ -39,19 +45,6 @@ end end - describe :p_inject do - it "exists on objects that support Enumerable" do - @ary.respond_to?(:p_each).should == true - end - it "behaves like inject" do - @ary.should.respond_to? :p_inject - end - - it "executes concurrently" do - true.should == true - end - end - describe :p_map do it "exists on objects that support Enumerable" do @ary.respond_to?(:p_each).should == true
participants (1)
-
source_changes@macosforge.org