Skip to content

Instantly share code, notes, and snippets.

@cromwellryan
Created March 1, 2014 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cromwellryan/9285584 to your computer and use it in GitHub Desktop.
Save cromwellryan/9285584 to your computer and use it in GitHub Desktop.
def pmap(enum)
return to_enum(:pmap, enum) unless block_given?
enum.each_with_index.map { |e,i| Thread.new { sleep(Random.rand); p e; yield ({index: i, value:e }) } }.map(&:value)
end
p to_enum(:pmap, 1..10).sort_by { |hash| hash[:index] }.map { |hash| hash[:value] }
#p pmap(1..10) { |x| x }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment