Skip to content

Commit 77861bf

Browse files
committed
pipes docs reorganization
1 parent ae19d31 commit 77861bf

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

docs/pipes.md

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ a |> then(&f(&1, ...)) |> b()
6464
a |> f(...) |> b()
6565
```
6666

67-
## Piped function optimizations
67+
## Optimizations
6868

6969
Two function calls into one! Fewer steps is always nice.
7070

@@ -120,7 +120,29 @@ a |> Enum.map(fun) |> Enum.intersperse(separator) |> ...
120120
a |> Enum.map_intersperse(separator, fun) |> ...
121121
```
122122

123-
## Unpiping Single Pipes
123+
### Req Optimizations
124+
125+
[Req](https://github.com/wojtekmach/req) is a popular HTTP Client. If you aren't using it, you can just ignore this whole section!
126+
127+
Styler ensures a minimal number of functions are being called when using any Req 1-arity execution functions (`delete get head patch post put request run` and their bangified versions).
128+
129+
```elixir
130+
# before
131+
keyword |> Req.new() |> Req.merge(opts) |> Req.post!()
132+
# Styled:
133+
Req.post!(keyword, opts)
134+
135+
# before
136+
foo |> Keyword.merge(opts) |> Req.head()
137+
# Styled:
138+
Req.head(foo, opts)
139+
```
140+
141+
**This changes the program's behaviour**, since `Keyword.merge` would overwrite existing values in all cases, whereas `Req` 2-arity functions intelligently deep-merge values for some keys, like `:headers`.
142+
143+
## Adding & Removing Pipes
144+
145+
### Unpiping Single Pipes
124146

125147
Styler rewrites pipechains with a single pipe to be function calls. Notably, this rule combined with the optimizations rewrites above means some chains with more than one pipe will also become function calls.
126148

@@ -134,7 +156,7 @@ map = a |> Enum.map(mapper) |> Map.new()
134156
map = Map.new(a, mapper)
135157
```
136158

137-
## Pipe-ify
159+
### Pipe-ify
138160

139161
If the first argument to a function call is a pipe, Styler makes the function call the final pipe of the chain.
140162

@@ -161,23 +183,3 @@ d(c(a |> b))
161183
# At which point Styler will pipe-ify the entire chain
162184
a |> b() |> c() |> d()
163185
```
164-
165-
## Req
166-
167-
[Req](https://github.com/wojtekmach/req) is a popular HTTP Client. If you aren't using it, you can just ignore this whole section!
168-
169-
Styler ensures a minimal number of functions are being called when using any Req 1-arity execution functions (`delete get head patch post put request run` and their bangified versions).
170-
171-
```elixir
172-
# before
173-
keyword |> Req.new() |> Req.merge(opts) |> Req.post!()
174-
# Styled:
175-
Req.post!(keyword, opts)
176-
177-
# before
178-
foo |> Keyword.merge(opts) |> Req.head()
179-
# Styled:
180-
Req.head(foo, opts)
181-
```
182-
183-
**This changes the program's behaviour**, since `Keyword.merge` would overwrite existing values in all cases, whereas `Req` 2-arity functions intelligently deep-merge values for some keys, like `:headers`.

docs/styles.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ Styler performs myriad rewrites, logically broken apart into the following group
88
- [General Styles](./general_styles.md): general simple 1-1 rewrites that require a minimum amount of awareness of the AST
99
- [Mix Configs](./mix_configs.md): Styler applies order to chaos by organizing mix `config ...` stanzas
1010
- [Module Directives](./module_directives.md): Styles for `alias`, `use`, `import`, `require`, as well as alias lifting and alias application.
11-
- [Pipes](./pipes.md) styles for the famous Elixir pipe `|>`, including optimizations for piping standard library functions
11+
- [Pipes](./pipes.md): Styles for the famous Elixir pipe `|>`, including optimizations for piping standard library functions
1212

1313
Finally, if you're using Credo [see our documentation](./credo.md) about rules that can be disabled in Credo because Styler automatically enforces them for you, saving a modicum of CI time.

0 commit comments

Comments
 (0)