Skip to content

Commit 47d3a18

Browse files
committed
Add remaining functions to Enum cheatsheet
1 parent 997d925 commit 47d3a18

File tree

1 file changed

+59
-15
lines changed

1 file changed

+59
-15
lines changed

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

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,6 @@ iex> Enum.flat_map_reduce(cart, 0, fn item, acc ->
350350
## Conversion
351351
{: .col-2}
352352

353-
### [to_list(enum)](`Enum.to_list/1`)
354-
355-
```elixir
356-
iex> Enum.to_list(1..5)
357-
[1, 2, 3, 4, 5]
358-
```
359-
360353
### [into(enum, collectable)](`Enum.into/2`)
361354

362355
```elixir
@@ -374,6 +367,13 @@ iex> Enum.into(cart, %{}, fn item ->
374367
%{"apple" => 3, "banana" => 1, "orange" => 6}
375368
```
376369

370+
### [to_list(enum)](`Enum.to_list/1`)
371+
372+
```elixir
373+
iex> Enum.to_list(1..5)
374+
[1, 2, 3, 4, 5]
375+
```
376+
377377
## Duplicates & uniques
378378
{: .col-2}
379379

@@ -427,48 +427,92 @@ iex> Enum.uniq_by(cart, &String.last(&1.fruit))
427427
### [at(enum, index, default \\\\ nil)](`Enum.at/2`)
428428

429429
```elixir
430-
iex> Enum.at(cart)
430+
iex> Enum.at(cart, 0)
431+
%{fruit: "apple", count: 3}
432+
iex> Enum.at(cart, 10)
433+
nil
434+
iex> Enum.at(cart, 10, :none)
435+
:none
431436
```
432437

438+
Accessing a list by index in a loop is discouraged.
439+
433440
### [fetch(enum, index)](`Enum.fetch/2`)
434441

435442
```elixir
436-
iex> Enum.fetch(cart)
443+
iex> Enum.fetch(cart, 0)
444+
{:ok, %{fruit: "apple", count: 3}}
445+
iex> Enum.fetch(cart, 10)
446+
:error
437447
```
438448

439-
### [fetch!(enum, index)](`Enum.fetch/2`)
449+
### [fetch!(enum, index)](`Enum.fetch!/2`)
440450

441451
```elixir
442-
iex> Enum.fetch(cart)
452+
iex> Enum.fetch!(cart, 0)
453+
%{fruit: "apple", count: 3}
454+
iex> Enum.fetch!(cart, 10)
455+
** (Enum.OutOfBoundsError) out of bounds error
443456
```
444457

445458
### [with_index(enum)](`Enum.with_index/1`)
446459

447460
```elixir
448461
iex> Enum.with_index(cart)
462+
[
463+
{%{fruit: "apple", count: 3}, 0},
464+
{%{fruit: "banana", count: 1}, 1},
465+
{%{fruit: "orange", count: 6}, 2}
466+
]
449467
```
450468

451-
See `Enum.with_index/2` for custom indexing.
469+
### [with_index(enum, fun)](`Enum.with_index/2`)
470+
471+
```elixir
472+
iex> Enum.with_index(cart, fun, fn item, index ->
473+
...> {item.fruit, index}
474+
...> end)
475+
[
476+
{"apple", 0},
477+
{"banana", 1},
478+
{"orange", 2}
479+
]
480+
```
452481

453482
## Finding
454483
{: .col-2}
455484

456485
### [find(enum, default \\\\ nil, fun)](`Enum.find/2`)
457486

458487
```elixir
459-
iex> Enum.find(cart)
488+
iex> Enum.find(cart, &(&1.fruit =~ "o"))
489+
%{fruit: "orange", count: 6}
490+
iex> Enum.find(cart, &(&1.fruit =~ "y"))
491+
nil
492+
iex> Enum.find(cart, :none, &(&1.fruit =~ "y"))
493+
:none
460494
```
461495

462496
### [find_index(enum, fun)](`Enum.find_index/2`)
463497

464498
```elixir
465-
iex> Enum.find_index(cart)
499+
iex> Enum.find_index(cart, &(&1.fruit =~ "o"))
500+
2
501+
iex> Enum.find_index(cart, &(&1.fruit =~ "y"))
502+
nil
466503
```
467504

468505
### [find_value(enum, default \\\\ nil, fun)](`Enum.find_value/2`)
469506

470507
```elixir
471-
iex> Enum.find_value(cart)
508+
iex> Enum.find_value(cart, fn item ->
509+
...> if item.count == 1, do: item.fruit, else: nil
510+
...> end)
511+
"banana"
512+
iex> Enum.find_value(cart, :none, fn item ->
513+
...> if item.count == 100, do: item.fruit, else: nil
514+
...> end)
515+
:none
472516
```
473517

474518
## Grouping

0 commit comments

Comments
 (0)