Skip to content

Commit 21cf25f

Browse files
committed
Add Pipe Into A Case Statement as an elixir til
1 parent 0907e55 commit 21cf25f

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-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-
_800 TILs and counting..._
13+
_801 TILs and counting..._
1414

1515
---
1616

@@ -147,6 +147,7 @@ _800 TILs and counting..._
147147
- [Match On A Map In A With Construct](elixir/match-on-a-map-in-a-with-construct.md)
148148
- [Passing Around And Using Modules](elixir/passing-around-and-using-modules.md)
149149
- [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)
150151
- [Quitting IEx](elixir/quitting-iex.md)
151152
- [Range Into List Using Comprehensions](elixir/range-into-list-using-comprehensions.md)
152153
- [Refer To A Module Within Itself](elixir/refer-to-a-module-within-itself.md)

elixir/pipe-into-a-case-statement.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.

0 commit comments

Comments
 (0)