~michalr/blog2gmi

639650abc3d28ff1c3b400fae6113902d8e23ade — MichaƂ Rudowicz 4 years ago d64c12f
A little cleanup
1 files changed, 6 insertions(+), 11 deletions(-)

M src/map_parallel.cr
M src/map_parallel.cr => src/map_parallel.cr +6 -11
@@ 1,15 1,10 @@
module Enumerable(T)
  def map_parallel(&block : T -> U) forall U
    wait = Channel(U).new
    num_fibers = 0
    self
      .tap { |x| num_fibers = x.size }
      .each do |x|
        spawn do
          wait.send(block.call(x))
        end
      end
    Array.new(num_fibers, nil)
      .map { |x| wait.receive }
    output_ch = Channel(U).new
    self.each do |x|
      spawn { output_ch.send(block.call(x)) }
    end
    Array.new(self.size, nil)
      .map { |x| output_ch.receive }
  end
end