@@ -99,6 +99,20 @@ iex> Enum.reject(cart, &(&1.fruit =~ "o"))
99
99
]
100
100
```
101
101
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
+
102
116
## Mapping
103
117
{: .col-2}
104
118
@@ -130,6 +144,26 @@ iex> Enum.map_every(cart, 2, fn item ->
130
144
]
131
145
```
132
146
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
+
133
167
## Side-effects
134
168
{: .col-2}
135
169
@@ -188,6 +222,26 @@ iex> Enum.reduce_while(cart, 0, fn item, acc ->
188
222
4
189
223
```
190
224
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
+
191
245
## Aggregations
192
246
{: .col-2}
193
247
@@ -303,7 +357,6 @@ When comparing structs, use `Enum.max/2` with a module as sorter.
303
357
304
358
### [max_by(enum, mapper)](`Enum.max_by/2`)
305
359
306
-
307
360
```elixir
308
361
iex> Enum.max_by(cart, & &1.count)
309
362
%{fruit: "orange", count: 6},
@@ -350,6 +403,19 @@ iex> Enum.flat_map_reduce(cart, 0, fn item, acc ->
350
403
"orange", "orange", "orange", "orange", "orange"], 10}
351
404
```
352
405
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
+
353
419
## Conversion
354
420
{: .col-2}
355
421
@@ -377,6 +443,17 @@ iex> Enum.to_list(1..5)
377
443
[1, 2, 3, 4, 5]
378
444
```
379
445
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
+
380
457
## Duplicates & uniques
381
458
{: .col-2}
382
459
@@ -412,6 +489,8 @@ iex> Enum.uniq([1, 2, 2, 3, 3, 3, 1, 2, 3])
412
489
[1, 2, 3]
413
490
```
414
491
492
+ Comprehensions also support the `uniq: true` option.
493
+
415
494
### [uniq_by(enum, fun)](`Enum.uniq_by/2`)
416
495
417
496
Get entries which are unique by the last letter of the fruit:
@@ -895,7 +974,7 @@ iex> Enum.zip(fruits, counts)
895
974
896
975
See `Enum.zip/1` for zipping many collections at once.
897
976
898
- ### [zip_with(enum1, enum2, zip_fun )](`Enum.zip_with/2`)
977
+ ### [zip_with(enum1, enum2, fun )](`Enum.zip_with/2`)
899
978
900
979
```elixir
901
980
iex> fruits = ["apple", "banana", "orange"]
@@ -912,7 +991,7 @@ iex> Enum.zip_with(fruits, counts, fn fruit, count ->
912
991
913
992
See `Enum.zip_with/2` for zipping many collections at once.
914
993
915
- ### [zip_reduce(left, right, acc, reducer )](`Enum.zip_reduce/4`)
994
+ ### [zip_reduce(left, right, acc, fun )](`Enum.zip_reduce/4`)
916
995
917
996
```elixir
918
997
iex> fruits = ["apple", "banana", "orange"]
0 commit comments