File tree 2 files changed +35
-1
lines changed
2 files changed +35
-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
- _ 800 TILs and counting..._
13
+ _ 801 TILs and counting..._
14
14
15
15
---
16
16
@@ -147,6 +147,7 @@ _800 TILs and counting..._
147
147
- [ Match On A Map In A With Construct] ( elixir/match-on-a-map-in-a-with-construct.md )
148
148
- [ Passing Around And Using Modules] ( elixir/passing-around-and-using-modules.md )
149
149
- [ Pattern Matching In Anonymous Functions] ( elixir/pattern-matching-in-anonymous-functions.md )
150
+ - [ Pipe Into A Case Statement] ( elixir/pipe-into-a-case-statement.md )
150
151
- [ Quitting IEx] ( elixir/quitting-iex.md )
151
152
- [ Range Into List Using Comprehensions] ( elixir/range-into-list-using-comprehensions.md )
152
153
- [ Refer To A Module Within Itself] ( elixir/refer-to-a-module-within-itself.md )
Original file line number Diff line number Diff line change
1
+ # Pipe Into A Case Statement
2
+
3
+ The standard use of a case statement looks something like this:
4
+
5
+ ``` elixir
6
+ case HTTPoison .get (url) do
7
+ {:ok , %HTTPoison .Response {status_code: 200 , body: body}} ->
8
+ IO .puts body
9
+ {:ok , %HTTPoison .Response {status_code: 404 }} ->
10
+ IO .puts " Not found :("
11
+ {:error , %HTTPoison .Error {reason: reason}} ->
12
+ IO .inspect reason
13
+ end
14
+ ```
15
+
16
+ If you are a fan of the pipe syntax, then you may enjoying writing the above
17
+ like this:
18
+
19
+ ``` elixir
20
+ url
21
+ |> HTTPoison .get ()
22
+ |> case do
23
+ {:ok , %HTTPoison .Response {status_code: 200 , body: body}} ->
24
+ IO .puts body
25
+ {:ok , %HTTPoison .Response {status_code: 404 }} ->
26
+ IO .puts " Not found :("
27
+ {:error , %HTTPoison .Error {reason: reason}} ->
28
+ IO .inspect reason
29
+ end
30
+ ```
31
+
32
+ Just like any function, the value from the previous line in the pipe will be
33
+ passed in and used as the value switched over in the case statement.
You can’t perform that action at this time.
0 commit comments