PEnum

Parallel Enum. This library provides a set of functions similar to the ones in the Enum module except that the function argument is executed on each element in parallel.

The behavior of each of the PEnum functions should be the same as the Enum varieties, except that order of execution is not guaranteed.

Except where otherwise noted, the function names are identical to the ones in Enum but with a p in front. For example, PEnum.pmap is a parallel version of Enum.map.

Installation

It is available in Hex and package can be installed by adding p_enum to your list of dependencies in mix.exs:

def deps do
  [
    {:p_enum, "~> 0.1.0"}
  ]
end

Documentation can be found at https://hexdocs.pm/p_enum.

Examples

expensive_function = fn n -> Enum.reduce(1..n, &Kernel.*/2) end

[30000, 40000, 50000] |> PEnum.pmap(expensive_function)
def numbers_less_than_five(enumerable) do
  enumerable
  |> PEnum.pfilter(fn n ->
    time = DateTime.utc_now()
    :timer.sleep(n * 1000)
    DateTime.diff(DateTime.utc_now(), time) < 5
  end)
end

Functions

The group_by family of functions

The Enum.group_by function takes two functions as arguments: a key_fun and a value_fun. Since someone may want to run either or both in parallel, there are three PEnum functions corresponding to Enum.group_by: