Skip to content

Commit 1eb152b

Browse files
committed
Add Using When Clauses In A With Construct as an elixir til
1 parent 1873dd8 commit 1eb152b

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
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-
_779 TILs and counting..._
13+
_780 TILs and counting..._
1414

1515
---
1616

@@ -159,6 +159,7 @@ _779 TILs and counting..._
159159
- [String Interpolation With Just About Anything](elixir/string-interpolation-with-just-about-anything.md)
160160
- [Unique Indexes With Ecto](elixir/unique-indexes-with-ecto.md)
161161
- [Updating Values In A Map](elixir/updating-values-in-a-map.md)
162+
- [Using When Clauses In A With Construct](elixir/using-when-clauses-in-a-with-construct.md)
162163
- [Virtual Fields With Ecto Schemas](elixir/virtual-fields-with-ecto-schemas.md)
163164
- [Word Lists For Atoms](elixir/word-lists-for-atoms.md)
164165

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Using When Clauses In A With Construct
2+
3+
Because Elixir's `with` construct supports the full power of the language's
4+
pattern matching, we can use `when` clauses to further narrow down our
5+
matches.
6+
7+
For instance, if we want to match against the response to an API request,
8+
but only for response status codes in the 2xx range, we can do something
9+
like the following:
10+
11+
```elixir
12+
with %{status_code: code, body: body}
13+
when code >= 200 && code < 300 <- HTTPoison.get!(url),
14+
{:ok, decoded_body} <- Poison.decode(body) do
15+
{:ok, decoded_body}
16+
end
17+
```
18+
19+
See the [docs for
20+
`with`](https://hexdocs.pm/elixir/Kernel.SpecialForms.html#with/1) for more
21+
details.

0 commit comments

Comments
 (0)