require 'generator'Sample usage:
# One mapcar to rule all Enumerables.
def mapcar(*enums)
generators = enums.collect {|e| Generator.new(e)}
result = []
while true
begin
params = generators.collect {|g| g.current; g.next}
rescue EOFError
return result
end
result << yield(*params)
end
end
qSorters = arrays.collect {QSort.new}This creates an Array of Threads that use the objects in qSorters to sort each element of arrays.
threads = mapcar(qSorters, arrays) {|q, a| Thread.new {q.quicksort(a)}}
Please note that the QSort class is user-defined and omitted here for brevity (and sanity).
The above mapcar is based on recipe 7.9, pages 256-257 of the Ruby Cookbook, specifically the interosculate function.
No comments:
Post a Comment