1
0
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:
jbranchaud
2019-03-14 16:21:20 -05:00
parent 687e4c53cf
commit ee7ad45086
2 changed files with 25 additions and 1 deletions

View 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.