Created
March 1, 2014 02:44
-
-
Save sferik/9284252 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def pmap(enum) | |
return to_enum(:pmap, enum) unless block_given? | |
enum.map { |e| Thread.new { yield e } }.map(&:value) | |
end | |
# Returns elements in order, as expected. | |
pmap(1..10) { |e| e } #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
# Returns elements in nondeterministic order on MRI >= 1.9.3. | |
# Works as expected on JRuby, Rubinius, and earlier versions of MRI. | |
pmap(1..10).to_a #=> [7, 2, 3, 4, 6, 5, 9, 8, 10, 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@mkdynamic @cromwellryan That makes sense. Thanks for explaining.
In case anyone else is curious, this is how I was able to get the correct ordering for
pmap
: