mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Compute Intermediate Values In A With Construct as an elixir til
This commit is contained in:
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
||||
For a steady stream of TILs from a variety of rocketeers, checkout
|
||||
[til.hashrocket.com](https://til.hashrocket.com/).
|
||||
|
||||
_782 TILs and counting..._
|
||||
_783 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -125,6 +125,7 @@ _782 TILs and counting..._
|
||||
- [Check For A Substring Match](elixir/check-for-a-substring-match.md)
|
||||
- [Check List Membership](elixir/check-list-membership.md)
|
||||
- [Comparing DateTime Structs](elixir/comparing-datetime-structs.md)
|
||||
- [Compute Intermediate Values In A With Construct](elixir/compute-intermediate-values-in-a-with-construct.md)
|
||||
- [Compute md5 Digest Of A String](elixir/compute-md5-digest-of-a-string.md)
|
||||
- [Counting Records With Ecto](elixir/counting-records-with-ecto.md)
|
||||
- [Create A Date With The Date Sigil](elixir/create-a-date-with-the-date-sigil.md)
|
||||
|
||||
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