mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add Match On A Map In A With Construct as an elixir til
This commit is contained in:
25
elixir/match-on-a-map-in-a-with-construct.md
Normal file
25
elixir/match-on-a-map-in-a-with-construct.md
Normal file
@@ -0,0 +1,25 @@
|
||||
# Match On A Map In A With Construct
|
||||
|
||||
Many usage example of the
|
||||
[`with`](https://hexdocs.pm/elixir/Kernel.SpecialForms.html#with/1)
|
||||
construct show a series of matches on a tuple.
|
||||
|
||||
```elixir
|
||||
with {:ok, width} <- Map.fetch(opts, :width),
|
||||
{:ok, height} <- Map.fetch(opts, :height) do
|
||||
{:ok, width * height}
|
||||
end
|
||||
```
|
||||
|
||||
You can match on more than just tuples though. Here is how you might match
|
||||
on a map.
|
||||
|
||||
```elixir
|
||||
with %{status_code: 200, body: body} <- HTTPoison.get!(url),
|
||||
{:ok, decoded_body} <- Poison.decode(body) do
|
||||
{:ok, decoded_body}
|
||||
end
|
||||
```
|
||||
|
||||
In fact, you have the full power of Elixir's pattern matching available to
|
||||
you in your series of matches for a `with` construct.
|
||||
Reference in New Issue
Block a user