Skip to content

Commit 682b6d5

Browse files
committed
Add comprehension examples
1 parent 3b92c0d commit 682b6d5

File tree

1 file changed

+82
-3
lines changed

1 file changed

+82
-3
lines changed

lib/elixir/pages/cheatsheets/enum-cheat.cheatmd

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ iex> Enum.reject(cart, &(&1.fruit =~ "o"))
9999
]
100100
```
101101

102+
### [Comprehension](`for/1`)
103+
104+
Filtering can also be done with comprehensions:
105+
106+
```elixir
107+
iex> for item <- cart, item.fruit =~ "e" do
108+
...> item
109+
...> end
110+
[
111+
%{fruit: "apple", count: 3},
112+
%{fruit: "orange", count: 6}
113+
]
114+
```
115+
102116
## Mapping
103117
{: .col-2}
104118

@@ -130,6 +144,26 @@ iex> Enum.map_every(cart, 2, fn item ->
130144
]
131145
```
132146

147+
### [Comprehension](`for/1`)
148+
149+
Mapping can also be done with comprehensions:
150+
151+
```elixir
152+
iex> for item <- cart do
153+
...> item.fruit
154+
...> end
155+
["apple", "banana", "orange"]
156+
```
157+
158+
You can also filter and map at once:
159+
160+
```elixir
161+
iex> for item <- cart, item.fruit =~ "e" do
162+
...> item.fruit
163+
...> end
164+
["apple", "orange"]
165+
```
166+
133167
## Side-effects
134168
{: .col-2}
135169

@@ -188,6 +222,26 @@ iex> Enum.reduce_while(cart, 0, fn item, acc ->
188222
4
189223
```
190224

225+
### [Comprehension](`for/1`)
226+
227+
Reducing can also be done with comprehensions:
228+
229+
```elixir
230+
iex> for item <- cart, reduce: 0 do
231+
...> acc -> item.count + acc
232+
...> end
233+
10
234+
```
235+
236+
You can also filter and reduce at once:
237+
238+
```elixir
239+
iex> for item <- cart, item.fruit =~ "e", reduce: 0 do
240+
...> acc -> item.count + acc
241+
...> end
242+
9
243+
```
244+
191245
## Aggregations
192246
{: .col-2}
193247

@@ -303,7 +357,6 @@ When comparing structs, use `Enum.max/2` with a module as sorter.
303357

304358
### [max_by(enum, mapper)](`Enum.max_by/2`)
305359

306-
307360
```elixir
308361
iex> Enum.max_by(cart, & &1.count)
309362
%{fruit: "orange", count: 6},
@@ -350,6 +403,19 @@ iex> Enum.flat_map_reduce(cart, 0, fn item, acc ->
350403
"orange", "orange", "orange", "orange", "orange"], 10}
351404
```
352405

406+
### [Comprehension](`for/1`)
407+
408+
Flattening can also be done with comprehensions:
409+
410+
```elixir
411+
iex> for item <- cart,
412+
...> fruit <- List.duplicate(item.fruit, item.count) do
413+
...> fruit
414+
...> end
415+
["apple", "apple", "apple", "banana", "orange",
416+
"orange", "orange", "orange", "orange", "orange"]
417+
```
418+
353419
## Conversion
354420
{: .col-2}
355421

@@ -377,6 +443,17 @@ iex> Enum.to_list(1..5)
377443
[1, 2, 3, 4, 5]
378444
```
379445

446+
### [Comprehension](`for/1`)
447+
448+
Conversion can also be done with comprehensions:
449+
450+
```elixir
451+
iex> for item <- cart, into: %{} do
452+
...> {item.fruit, item.count}
453+
...> end
454+
%{"apple" => 3, "banana" => 1, "orange" => 6}
455+
```
456+
380457
## Duplicates & uniques
381458
{: .col-2}
382459

@@ -412,6 +489,8 @@ iex> Enum.uniq([1, 2, 2, 3, 3, 3, 1, 2, 3])
412489
[1, 2, 3]
413490
```
414491

492+
Comprehensions also support the `uniq: true` option.
493+
415494
### [uniq_by(enum, fun)](`Enum.uniq_by/2`)
416495

417496
Get entries which are unique by the last letter of the fruit:
@@ -895,7 +974,7 @@ iex> Enum.zip(fruits, counts)
895974

896975
See `Enum.zip/1` for zipping many collections at once.
897976

898-
### [zip_with(enum1, enum2, zip_fun)](`Enum.zip_with/2`)
977+
### [zip_with(enum1, enum2, fun)](`Enum.zip_with/2`)
899978

900979
```elixir
901980
iex> fruits = ["apple", "banana", "orange"]
@@ -912,7 +991,7 @@ iex> Enum.zip_with(fruits, counts, fn fruit, count ->
912991

913992
See `Enum.zip_with/2` for zipping many collections at once.
914993

915-
### [zip_reduce(left, right, acc, reducer)](`Enum.zip_reduce/4`)
994+
### [zip_reduce(left, right, acc, fun)](`Enum.zip_reduce/4`)
916995

917996
```elixir
918997
iex> fruits = ["apple", "banana", "orange"]

0 commit comments

Comments
 (0)