Skip to content

Commit ee7ad45

Browse files
committed
Add Compute Intermediate Values In A With Construct as an elixir til
1 parent 687e4c5 commit ee7ad45

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
1010
For a steady stream of TILs from a variety of rocketeers, checkout
1111
[til.hashrocket.com](https://til.hashrocket.com/).
1212

13-
_782 TILs and counting..._
13+
_783 TILs and counting..._
1414

1515
---
1616

@@ -125,6 +125,7 @@ _782 TILs and counting..._
125125
- [Check For A Substring Match](elixir/check-for-a-substring-match.md)
126126
- [Check List Membership](elixir/check-list-membership.md)
127127
- [Comparing DateTime Structs](elixir/comparing-datetime-structs.md)
128+
- [Compute Intermediate Values In A With Construct](elixir/compute-intermediate-values-in-a-with-construct.md)
128129
- [Compute md5 Digest Of A String](elixir/compute-md5-digest-of-a-string.md)
129130
- [Counting Records With Ecto](elixir/counting-records-with-ecto.md)
130131
- [Create A Date With The Date Sigil](elixir/create-a-date-with-the-date-sigil.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Compute Intermediate Values In A With Construct
2+
3+
The expressions you use in a `with` construct do not have to contain the
4+
`<-` syntax. You can pattern match and bind values along the way as well.
5+
6+
```elixir
7+
with %{id: id} <- get_user()
8+
url = "/api/#{id}/blogs",
9+
%{status_code: 200, body: body} <- HTTPoison.get(url),
10+
{:ok, decoded_body} <- Poison.decode(body) do
11+
{:ok, decoded_body}
12+
end
13+
```
14+
15+
In the above (sorta contrived) example we were able to construct a URL in
16+
the middle of the series of expressions.
17+
18+
The values we compute inline will be closed into the `with` construct, so
19+
they won't leak.
20+
21+
See the [`with`
22+
docs](https://hexdocs.pm/elixir/Kernel.SpecialForms.html#with/1) for more
23+
details.

0 commit comments

Comments
 (0)