daily pastebin goal
75%
SHARE
TWEET

Untitled

a guest Jun 22nd, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'rubygems'
  2. require 'eventmachine'
  3. require 'enumerator'
  4.  
  5. module EventMachine
  6.   # Iteration helpers for asynchronous code
  7.   module Iterator
  8.     def self.mapreduce(enum, map = nil, reduce = nil)
  9.       if enum.any?
  10.         num = enum.size
  11.         results = []
  12.  
  13.         iterator = nil
  14.  
  15.         done = proc{ |r|
  16.           results << r
  17.           if (num -= 1) == 0
  18.             reduce.call(results)
  19.           else
  20.             iterator.call
  21.           end
  22.         }
  23.  
  24.         iterator = proc{
  25.           entry = enum.shift
  26.           map.call(entry, done)
  27.         }
  28.         iterator.call
  29.       end
  30.     end
  31.   end
  32. end
  33.  
  34. EM.run do
  35.   EM::Iterator.mapreduce([1,2,3], proc{ |n, done|
  36.     EM.add_timer(1){
  37.       done.call([n+1, Time.now])
  38.     }
  39.   }, proc{ |results|
  40.     p results
  41.   })
  42. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top