module Enumerable

Public Instance Methods

clustered_threaded_collect(number_of_clusters, &block) click to toggle source

Like Enumerable#collect(), but all blocks are clustered. Each cluster is run concurrently in a thread, using ThreadLimiter.new(number_of_clusters) and its fork(). Set number_of_clusters to -1 to skip clustering.

# File lib/threadlimiter/enumerable.rb, line 27
def clustered_threaded_collect(number_of_clusters, &block)
  if number_of_clusters <= 0
    threaded_collect(number_of_clusters, &block)
  else
    ThreadLimiter.handle_clusters(self, number_of_clusters, :threaded_collect, &block)
  end
end
Also aliased as: clustered_threaded_map
clustered_threaded_each(number_of_clusters, &block) click to toggle source

Like Enumerable#each(), but all blocks are clustered. Each cluster is run concurrently in a thread, using ThreadLimiter.new(number_of_clusters) and its fork(). Set number_of_clusters to -1 to skip clustering.

# File lib/threadlimiter/enumerable.rb, line 90
def clustered_threaded_each(number_of_clusters, &block)
  clustered_threaded_collect(number_of_clusters, &block)

  self
end
clustered_threaded_map(number_of_clusters, &block) click to toggle source
clustered_threaded_reject(number_of_clusters, &block) click to toggle source

Like Enumerable#reject(), but all blocks are clustered. Each cluster is run concurrently in a thread, using ThreadLimiter.new(number_of_clusters) and its fork(). Set number_of_clusters to -1 to skip clustering.

# File lib/threadlimiter/enumerable.rb, line 82
def clustered_threaded_reject(number_of_clusters, &block)
  self.zip(self.clustered_threaded_collect(number_of_clusters, &block)).inject([]){|r, (o, b)| r << o unless b ; r}
end
clustered_threaded_select(number_of_clusters, &block) click to toggle source

Like Enumerable#select(), but all blocks are clustered. Each cluster is run concurrently in a thread, using ThreadLimiter.new(number_of_clusters) and its fork(). Set number_of_clusters to -1 to skip clustering.

# File lib/threadlimiter/enumerable.rb, line 74
def clustered_threaded_select(number_of_clusters, &block)
  self.zip(self.clustered_threaded_collect(number_of_clusters, &block)).inject([]){|r, (o, b)| r << o if b ; r}
end
threaded_collect(limit=-1, &block) click to toggle source

Like Enumerable#collect(), but each block is run concurrently in a thread, using ThreadLimiter.new(limit) and its fork(). Set limit to 0 to use plain old collect() without any threading.

# File lib/threadlimiter/enumerable.rb, line 5
def threaded_collect(limit=-1, &block)
  if limit == 0
    self.collect(&block)
  else
    thread_limiter    = ThreadLimiter.new(limit)

    self.collect do |object|
      if block.arity > 1 and object.kind_of?(Enumerable)
        thread_limiter.fork(*object.to_a, &block)
      else
        thread_limiter.fork(object, &block)
      end
    end.collect do |thread|
      thread.value
    end
  end
end
Also aliased as: threaded_map
threaded_each(limit=-1, &block) click to toggle source

Like Enumerable#each(), but each block is run concurrently in a thread, using ThreadLimiter.new(limit) and its fork(). Set limit to 0 to use plain old each() without any threading.

# File lib/threadlimiter/enumerable.rb, line 60
def threaded_each(limit=-1, &block)
  if limit == 0
    self.each(&block)
  else
    threaded_collect(limit, &block)

    self
  end
end
threaded_map(limit=-1, &block) click to toggle source
Alias for: threaded_collect
threaded_reject(limit=-1, &block) click to toggle source

Like Enumerable#reject(), but each block is run concurrently in a thread, using ThreadLimiter.new(limit) and its fork(). Set limit to 0 to use plain old reject() without any threading.

# File lib/threadlimiter/enumerable.rb, line 49
def threaded_reject(limit=-1, &block)
  if limit == 0
    self.reject(&block)
  else
    self.zip(self.threaded_collect(limit, &block)).inject([]){|r, (o, b)| r << o unless b ; r}
  end
end
threaded_select(limit=-1, &block) click to toggle source

Like Enumerable#select(), but each block is run concurrently in a thread, using ThreadLimiter.new(limit) and its fork(). Set limit to 0 to use plain old select() without any threading.

# File lib/threadlimiter/enumerable.rb, line 38
def threaded_select(limit=-1, &block)
  if limit == 0
    self.select(&block)
  else
    self.zip(self.threaded_collect(limit, &block)).inject([]){|r, (o, b)| r << o if b ; r}
  end
end