Created
November 15, 2018 15:53
-
-
Save nathan-cruz77/07ac71d8bb20ec229c8892e2b53c6837 to your computer and use it in GitHub Desktop.
Utility functions to ease working with parallel processes in elixir.
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
| 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