A quick pattern matching review
iex> {:ok, result} = {:ok, 13}
{:ok, 13}
iex> result
13iex> x = 1
1
iex> x
1
iex> 1 = x
1
iex> 2 = x
** (MatchError) no match of right hand side value: 1
iex> {:ok, result} = {:ok, 13}
{:ok, 13}
iex> result
131..500 |>
Enum.map(fn x -> x / 2 end) |>
Enum.filter(fn x -> x > 200 end) |>
Enum.countLet's look at some examples from Jose:
with {:ok, x} <- ok(1),
{:ok, y} <- ok(2),
do: {:ok, x + y}
#=> {:ok, 3}If they all match in the sequence, we're done.
with {:ok, x} <- ok(1),
{:ok, y} <- error(2),
do: {:ok, x + y}
#=> {:error, 2}Here, because they don't, the sequence is aborted and an error is returned.
def handle_request(request) do
with {:ok} <- validate_request(request),
{:ok, user} <- get_user(request),
{:ok} <- update_db(user),
{:ok} <- send_email(user) do
return_http_message
else
{:error, reason} -> handle_error(reason)
_ -> handle_ambigous_error
# alternately, you could handle the errors
# {:error, :update_db, details} -> handle_update_db_error(details)
# {:error, :send_email, details} -> handle_send_email_error(details)
end
end opts = %{width: 10, height: 15}
assert {:ok, 150} ==
with {:ok, width} <- Map.fetch(opts, :width),
{:ok, height} <- Map.fetch(opts, :height),
do: {:ok, width * height}case File.read(path) do
{:ok, binary} ->
case :beam_lib.chunks(binary, :abstract_code) do
{:ok, data} ->
{:ok, wrap(data)}
error ->
error
end
error ->
error
endwith {:ok, binary} <- File.read(path),
{:ok, data} <- :beam_lib.chunks(binary, :abstract_code),
do: {:ok, wrap(data)}Can become: