Skip to content

Instantly share code, notes, and snippets.

@Markonis
Created March 18, 2019 15:29
Show Gist options
  • Save Markonis/772808f1b86d088c63abd318d5d109a4 to your computer and use it in GitHub Desktop.
Save Markonis/772808f1b86d088c63abd318d5d109a4 to your computer and use it in GitHub Desktop.
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