[macruby-changes] [3601] MacRuby/trunk/lib/dispatch

source_changes at macosforge.org source_changes at macosforge.org
Wed Feb 24 13:23:41 PST 2010


Revision: 3601
          http://trac.macosforge.org/projects/ruby/changeset/3601
Author:   ernest.prabhakar at gmail.com
Date:     2010-02-24 13:23:41 -0800 (Wed, 24 Feb 2010)
Log Message:
-----------
Changed Dispatch Enumerable#p_mapreduce to take a stride

Modified Paths:
--------------
    MacRuby/trunk/lib/dispatch/README.rdoc
    MacRuby/trunk/lib/dispatch/enumerable.rb

Modified: MacRuby/trunk/lib/dispatch/README.rdoc
===================================================================
--- MacRuby/trunk/lib/dispatch/README.rdoc	2010-02-24 21:23:30 UTC (rev 3600)
+++ MacRuby/trunk/lib/dispatch/README.rdoc	2010-02-24 21:23:41 UTC (rev 3601)
@@ -179,16 +179,16 @@
 	
 Note that even though the iterator as a whole is synchronous, each block is independent and may execute out of order.
 
-Even this add noticeable overhead compared to the non-parallel version, so if you have a large number of relatively cheap iterations you can batch them together by specifying a +stride+:
+This may add noticeable overhead compared to the non-parallel version, so if you have a large number of relatively cheap iterations you can batch them together by specifying a +stride+:
 
 	5.p_times(3) { |i| puts 10**i } # =>1000 10000 1 10 100 
 
 It doesn't change the result, but schedules fewer blocks thus amortizing the overhead over more work. Note that items _within_ a stride are executed in the original order, but no order is guaranteed _between_ strides.
 
 === Enumerable ===
-	
-The rest are all defined on +Enumerable+, so they are available from any class which mixes that in (e.g, +Array+, +Hash+, etc.). These also can take an optional stride.
 
+The +p_times+ method is used to implement several convenience methods on +Enumerable+, which are therefore available from any class which mixes that in (e.g, +Array+, +Hash+, etc.). These also can take an optional stride.
+
 ==== p_each ====
 
 	%w(Mon Tue Wed Thu Fri).p_each { |day| puts day} # => Mon Wed Thu Tue Fri
@@ -213,14 +213,15 @@
 
 	(0..4).p_mapreduce(0) { |i| 10**i } # => 11111
 
-This uses a parallel +inject+ (formerly known as +reduce+) to return a single value by combining the result of +map+ by repeatedly sending ":+" message to update the result.  Unlike +inject+, you must specify an explicit initial value; unlike +p_map+, it does not take a stride.
+This uses a parallel +inject+ (formerly known as +reduce+) to return a single value by combining the result of +map+. 	Unlike +inject+, you must specify an explicit initial value. The default accumulator is ":+", but you can specify a different symbol to +send+:
 
-You can also pass any method as a symbol to use with the accumulator:
-
-	(0..2).p_mapreduce(0, :*) { |i| 10**i } # => 1000
+	(0..4).p_mapreduce([], :concat) { |i| [10**i] } # => [1, 1000, 10, 100, 10000]
 	
-Note that there is no stride.
+Because of those parameters, the optional +stride+ is now the third:
 
+	(0..4).p_mapreduce([], :concat, 3) { |i| [10**i] } # => [1000, 10000, 1, 10, 100]
+
+
 ==== p_find ====
 
 == Events

Modified: MacRuby/trunk/lib/dispatch/enumerable.rb
===================================================================
--- MacRuby/trunk/lib/dispatch/enumerable.rb	2010-02-24 21:23:30 UTC (rev 3600)
+++ MacRuby/trunk/lib/dispatch/enumerable.rb	2010-02-24 21:23:41 UTC (rev 3601)
@@ -62,7 +62,7 @@
   # Parallel +collect+ plus +inject+
   # Accumulates from +initial+ via +op+ (default = '+')
   # Note: each object will only run one p_mapreduce at a time
-  def p_mapreduce(initial, op=:+, &block)
+  def p_mapreduce(initial, op=:+, stride=1, priority=nil, &block)
     # Check first, since exceptions from a Dispatch block can act funky 
     raise ArgumentError if not initial.respond_to? op
     # TODO: assign from within a Dispatch.once to avoid race condition
@@ -70,7 +70,7 @@
     @mapreduce_q.sync do # in case called more than once at a time
       @mapreduce_result = initial
       q = Dispatch::Queue.for(@mapreduce_result)
-      self.p_each do |obj|
+      self.p_each(stride, priority) do |obj|
         val = block.call(obj)
         q.async { @mapreduce_result = @mapreduce_result.send(op, val) }
       end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100224/7bbdc09f/attachment.html>


More information about the macruby-changes mailing list