Created
March 18, 2019 15:29
-
-
Save Markonis/772808f1b86d088c63abd318d5d109a4 to your computer and use it in GitHub Desktop.
This file contains 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
defmodule Parallel do | |
def map(enumerable, fun) do | |
parent = self | |
chunk_size = chunk_size(enumerable) | |
enumerable | |
|> Enum.chunk(chunk_size, chunk_size, []) | |
|> Enum.map(fn chunk -> | |
spawn fn -> map_chunk(parent, chunk, fun) end | |
end) | |
|> Enum.flat_map(fn pid -> | |
receive do {^pid, chunk_result} -> chunk_result end | |
end) | |
end | |
# def each removed for brevity... | |
def chunk_size(enumerable) do | |
num_cores = :erlang.system_info(:logical_processors) | |
Enum.max [div(length(enumerable), num_cores), 1] | |
end | |
defp map_chunk(parent, chunk, fun) do | |
result = Enum.map chunk, fn item -> fun.(item) end | |
send(parent, {self, result}) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment