Proposal to expand Enum.split_with/2 with post-process function option similar to how it implemented in Enum.group_by/3 with third argument.
In my experience it is an often case when you need to split a enum through a function with a spec like:
@spec foo(term) :: {:ok, result_t} | {:error, error_t}
and receive two enums, where one contains successful results and another contains errors like: {list(result_t), list(error_t)}.
Enum.map/2 + Enum.split_by/2 seems to be a good choice for this work but unfortunately split_by it doesn't support post-process function (like third argument of Enum.group_by/3 does) so result of applying Enum.split_with/2 will be: {list({:ok, result_t}, list({:error, error_t})} instead.
I propose to expand Enum.split_with to this signature:
split_with(enumerable, split_fun, transform_fun \\ fn x -> x end)
so it can be used like this:
{successful_results, errors} =
mu_enum
|> Enum.map(&foo/1)
|> Enum.split_by(& match?({:ok, _}, &1), fn {_, res} -> res end)
Because of lack of transform_fun in current implementation an additional step needed:
{Enum.map(successful_results, fn {_, res} -> res end), Enum.map(errors, fn {_, res} -> res end)}.