Skip to content

Instantly share code, notes, and snippets.

@nathan-cruz77
Created November 15, 2018 15:53
Show Gist options
  • Select an option

  • Save nathan-cruz77/07ac71d8bb20ec229c8892e2b53c6837 to your computer and use it in GitHub Desktop.

Select an option

Save nathan-cruz77/07ac71d8bb20ec229c8892e2b53c6837 to your computer and use it in GitHub Desktop.
Utility functions to ease working with parallel processes in elixir.
defmodule Parallel do
@moduledoc """
Utility functions to ease working with parallel processes.
"""
@doc """
Equivalent to `Enum.map/2` but each `function` call is made inside its own
process.
"""
def map(enumerable, function) do
enumerable
|> Enum.map(&Task.async(fn -> function.(&1) end))
|> Enum.map(&Task.await/1)
end
@doc """
Run a list of functions in parallel and return their results in a
list.
"""
def run_tasks(tasks) do
tasks
|> Enum.map(&Task.async/1)
|> Enum.map(&Task.await/1)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment