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"}
]
endDocumentation 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)
endFunctions
pchunk_by/2pcount/2pdedup_by/2peach/2pfilter/2pgroup_by/2pgroup_by/3group_byp/2group_byp/3pgroup_byp/2pgroup_byp/3pflat_map/2pinto/3pmap/2pmap_every/3pmap_join/2pmap_join/3pmax_by/2pmax_by/3pmin_by/2pmin_by/3pmin_max_by/2pmin_max_by/3preject/2puniq_by/2
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:
pgroup_by- runs onlykey_funin parallelgroup_byp- runs onlyvalue_funin parallelpgroup_byp- runs bothkey_funandvalue_funin parallel