Permalink
Browse files

import of blaine's mapreducerb

  • Loading branch information...
0 parents commit d65a05002920fdaceed754864fd92cbe37f8ebd3 @defunkt committed Apr 26, 2008
Showing with 59 additions and 0 deletions.
  1. +16 −0 README
  2. +26 −0 mapreduce_enumerable.rb
  3. +17 −0 mapreduce_runner.rb
@@ -0,0 +1,16 @@
+By Blaine Cook.
+
+http://romeda.org/blog/2007/04/mapreduce-in-36-lines-of-ruby.html
+
+If you want to add `dinject` (reduce) please do.
+
+In your shell:
+$ sudo gem install RingyDingy
+$ sudo gem install ruby2ruby
+$ ring_server &
+$ ruby mapreduce_runner &
+$ ruby mapreduce_runner &
+
+In IRB:
+>> require 'mapreduce_enumerable'
+>> (1..100).to_a.dmap { |v| v * 2 }
@@ -0,0 +1,26 @@
+require 'rubygems'
+require 'ringy_dingy'
+require 'ruby2ruby'
+
+module Enumerable
+ def dmap(&block)
+ self.each_with_index do |element,idx|
+ ring_server.write([:dmap, Process.pid, block.to_ruby, element, idx])
+ end
+
+ results = []
+ while results.size < self.size
+ result, idx = ring_server.take([:dmap, Process.pid, nil, nil]).last(2)
+ results[idx] = result
+ end
+
+ results
+ end
+
+ def ring_server
+ return @ring_server if @ring_server
+
+ ringy_dingy = RingyDingy.new nil
+ @ring_server = ringy_dingy.ring_server
+ end
+end
@@ -0,0 +1,17 @@
+require 'rubygems'
+require 'ruby2ruby'
+require 'ringy_dingy'
+
+ringy_dingy = RingyDingy.new nil
+ring_server = ringy_dingy.ring_server
+
+loop do
+ pid, block, element, idx = ring_server.take([:dmap, nil, nil, nil, nil]).last(4)
+ begin
+ result = eval(block).call(element)
+ rescue Object => err
+ result = err
+ end
+ puts "Got #{result} from #{element} for #{pid}."
+ ring_server.write([:dmap, pid, result, idx])
+end

0 comments on commit d65a050

Please sign in to comment.