mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 15:18:01 +00:00
Add Compute Intermediate Values In A With Construct as an elixir til
This commit is contained in:
23
elixir/compute-intermediate-values-in-a-with-construct.md
Normal file
23
elixir/compute-intermediate-values-in-a-with-construct.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# Compute Intermediate Values In A With Construct
|
||||
|
||||
The expressions you use in a `with` construct do not have to contain the
|
||||
`<-` syntax. You can pattern match and bind values along the way as well.
|
||||
|
||||
```elixir
|
||||
with %{id: id} <- get_user()
|
||||
url = "/api/#{id}/blogs",
|
||||
%{status_code: 200, body: body} <- HTTPoison.get(url),
|
||||
{:ok, decoded_body} <- Poison.decode(body) do
|
||||
{:ok, decoded_body}
|
||||
end
|
||||
```
|
||||
|
||||
In the above (sorta contrived) example we were able to construct a URL in
|
||||
the middle of the series of expressions.
|
||||
|
||||
The values we compute inline will be closed into the `with` construct, so
|
||||
they won't leak.
|
||||
|
||||
See the [`with`
|
||||
docs](https://hexdocs.pm/elixir/Kernel.SpecialForms.html#with/1) for more
|
||||
details.
|
||||
Reference in New Issue
Block a user