lib/twitter/utils.rb
module Twitter
module Utils
module_function
# Returns a new array with the concatenated results of running block once for every element in enumerable.
# If no block is given, an enumerator is returned instead.
#
# @param enumerable [Enumerable]
# @return [Array, Enumerator]
def flat_pmap(enumerable, &block)
return to_enum(:flat_pmap, enumerable) unless block
pmap(enumerable, &block).flatten(1)
end
# Returns a new array with the results of running block once for every element in enumerable.
# If no block is given, an enumerator is returned instead.
#
# @param enumerable [Enumerable]
# @return [Array, Enumerator]
def pmap(enumerable, &block)
return to_enum(:pmap, enumerable) unless block
if enumerable.count == 1
enumerable.collect(&block)
else
enumerable.collect { |object| Thread.new { yield(object) } }.collect(&:value)
end
end
end
end