Packages

Elixir utils.

Retired package: Deprecated - Use instead antl_utils_* packages

Current section

Files

Jump to
antl_utils lib elixir enum.ex
Raw

lib/elixir/enum.ex

defmodule AntlUtils.Elixir.Enum do
@doc """
Invokes the given fun for each item in the enumerable.
Returns the value of the function that match the pattern {:error, _} otherwise return :ok
## Examples
iex> [1, 2, 3] |> AntlUtils.Elixir.Enum.perform(fn x -> {:error, x} end)
{:error, 1}
iex> [1, 2, 3] |> AntlUtils.Elixir.Enum.perform(fn _x -> :ok end)
:ok
"""
@spec perform(any, (any -> any)) :: any
def perform(enumerable, fun) do
enumerable
|> Enum.map(fun)
|> Enum.filter(&match?({:error, _}, &1))
|> case do
[] ->
:ok
errors_list when is_list(errors_list) ->
errors_list |> List.first()
end
end
end