mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 08:08:02 +00:00
Add Using When Clauses 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
|
For a steady stream of TILs from a variety of rocketeers, checkout
|
||||||
[til.hashrocket.com](https://til.hashrocket.com/).
|
[til.hashrocket.com](https://til.hashrocket.com/).
|
||||||
|
|
||||||
_779 TILs and counting..._
|
_780 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -159,6 +159,7 @@ _779 TILs and counting..._
|
|||||||
- [String Interpolation With Just About Anything](elixir/string-interpolation-with-just-about-anything.md)
|
- [String Interpolation With Just About Anything](elixir/string-interpolation-with-just-about-anything.md)
|
||||||
- [Unique Indexes With Ecto](elixir/unique-indexes-with-ecto.md)
|
- [Unique Indexes With Ecto](elixir/unique-indexes-with-ecto.md)
|
||||||
- [Updating Values In A Map](elixir/updating-values-in-a-map.md)
|
- [Updating Values In A Map](elixir/updating-values-in-a-map.md)
|
||||||
|
- [Using When Clauses In A With Construct](elixir/using-when-clauses-in-a-with-construct.md)
|
||||||
- [Virtual Fields With Ecto Schemas](elixir/virtual-fields-with-ecto-schemas.md)
|
- [Virtual Fields With Ecto Schemas](elixir/virtual-fields-with-ecto-schemas.md)
|
||||||
- [Word Lists For Atoms](elixir/word-lists-for-atoms.md)
|
- [Word Lists For Atoms](elixir/word-lists-for-atoms.md)
|
||||||
|
|
||||||
|
|||||||
21
elixir/using-when-clauses-in-a-with-construct.md
Normal file
21
elixir/using-when-clauses-in-a-with-construct.md
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
# Using When Clauses In A With Construct
|
||||||
|
|
||||||
|
Because Elixir's `with` construct supports the full power of the language's
|
||||||
|
pattern matching, we can use `when` clauses to further narrow down our
|
||||||
|
matches.
|
||||||
|
|
||||||
|
For instance, if we want to match against the response to an API request,
|
||||||
|
but only for response status codes in the 2xx range, we can do something
|
||||||
|
like the following:
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
with %{status_code: code, body: body}
|
||||||
|
when code >= 200 && code < 300 <- HTTPoison.get!(url),
|
||||||
|
{:ok, decoded_body} <- Poison.decode(body) do
|
||||||
|
{:ok, decoded_body}
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
See the [docs for
|
||||||
|
`with`](https://hexdocs.pm/elixir/Kernel.SpecialForms.html#with/1) for more
|
||||||
|
details.
|
||||||
Reference in New Issue
Block a user