Skip to content

Instantly share code, notes, and snippets.

@hanachin
Created April 15, 2020 13:06
Show Gist options
  • Save hanachin/c965f5a3608ef09796a9c618a12ebb72 to your computer and use it in GitHub Desktop.
Save hanachin/c965f5a3608ef09796a9c618a12ebb72 to your computer and use it in GitHub Desktop.
Array#ractor_map
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 }
@ioquatix
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment