How to compile a list of strings to regex expressions and stop in case of error?

# opts = %{skip: ["regex expression"]}
def start_link(opts) do
  Map.get(opts, :skip, %{})
  |> Enum.map(&compile_regex/1)

  GenServer.start_link(__MODULE__, 0)
end

defp compile_regex(entry) do
  case Regex.compile(entry) do
    {:ok, expr} -> expr
    {:error, reason} -> {:error, reason}
  end
end

The problem with this approach is that I’ll be parsing new entries even if there is already a failure. How can I stop on the first failure?

Reduce while solves the problem. I was looking at an older Elixir docs :sweat:

1 Like