Skip to content

Instantly share code, notes, and snippets.

@asterite
Created March 12, 2015 15:46
Show Gist options
  • Save asterite/40cf125b8ff6aaf95ac4 to your computer and use it in GitHub Desktop.
Save asterite/40cf125b8ff6aaf95ac4 to your computer and use it in GitHub Desktop.
module Enumerable
def parallel_map(&block : T -> U)
channel = Channel(U).new(20)
count = 0
each do |e|
spawn parallel_map_task(channel, block, e)
count += 1
end
Array(U).new(count) { channel.receive }
end
private def parallel_map_task(channel, block, element)
channel.send block.call(element)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment