Created
April 15, 2020 13:06
-
-
Save hanachin/c965f5a3608ef09796a9c618a12ebb72 to your computer and use it in GitHub Desktop.
Array#ractor_map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Module.new { | |
refine(Array) do | |
def ractor_map(&block) | |
rs = map.with_index do |x, index| | |
r = Ractor.new(x, &block) | |
Ractor.new(r, index) do |r, index| | |
[r.take, index] | |
end | |
end | |
result = [] | |
until rs.empty? | |
r, ret = Ractor.select(*rs) | |
rs.delete(r) | |
result[ret[1]] = ret[0] | |
end | |
result | |
end | |
end | |
} | |
ns = 10.times.to_a | |
p ns.map { sleep 1; _1 * _1 } | |
p ns.ractor_map { sleep 1; _1 * _1 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the same but for async: https://github.com/socketry/async-await/blob/master/lib/async/await/enumerable.rb