File tree 2 files changed +23
-1
lines changed 2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
10
10
For a steady stream of TILs from a variety of rocketeers, checkout
11
11
[ til.hashrocket.com] ( https://til.hashrocket.com/ ) .
12
12
13
- _ 779 TILs and counting..._
13
+ _ 780 TILs and counting..._
14
14
15
15
---
16
16
@@ -159,6 +159,7 @@ _779 TILs and counting..._
159
159
- [ String Interpolation With Just About Anything] ( elixir/string-interpolation-with-just-about-anything.md )
160
160
- [ Unique Indexes With Ecto] ( elixir/unique-indexes-with-ecto.md )
161
161
- [ 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 )
162
163
- [ Virtual Fields With Ecto Schemas] ( elixir/virtual-fields-with-ecto-schemas.md )
163
164
- [ Word Lists For Atoms] ( elixir/word-lists-for-atoms.md )
164
165
Original file line number Diff line number Diff line change
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.
You can’t perform that action at this time.
0 commit comments