Skip to content

Commit

Permalink
Add comprehension examples
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Aug 22, 2023
1 parent 3b92c0d commit 682b6d5
Showing 1 changed file with 82 additions and 3 deletions.
85 changes: 82 additions & 3 deletions lib/elixir/pages/cheatsheets/enum-cheat.cheatmd
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ iex> Enum.reject(cart, &(&1.fruit =~ "o"))
]
```

### [Comprehension](`for/1`)

Filtering can also be done with comprehensions:

```elixir
iex> for item <- cart, item.fruit =~ "e" do
...> item
...> end
[
%{fruit: "apple", count: 3},
%{fruit: "orange", count: 6}
]
```

## Mapping
{: .col-2}

Expand Down Expand Up @@ -130,6 +144,26 @@ iex> Enum.map_every(cart, 2, fn item ->
]
```

### [Comprehension](`for/1`)

Mapping can also be done with comprehensions:

```elixir
iex> for item <- cart do
...> item.fruit
...> end
["apple", "banana", "orange"]
```

You can also filter and map at once:

```elixir
iex> for item <- cart, item.fruit =~ "e" do
...> item.fruit
...> end
["apple", "orange"]
```

## Side-effects
{: .col-2}

Expand Down Expand Up @@ -188,6 +222,26 @@ iex> Enum.reduce_while(cart, 0, fn item, acc ->
4
```

### [Comprehension](`for/1`)

Reducing can also be done with comprehensions:

```elixir
iex> for item <- cart, reduce: 0 do
...> acc -> item.count + acc
...> end
10
```

You can also filter and reduce at once:

```elixir
iex> for item <- cart, item.fruit =~ "e", reduce: 0 do
...> acc -> item.count + acc
...> end
9
```

## Aggregations
{: .col-2}

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

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


```elixir
iex> Enum.max_by(cart, & &1.count)
%{fruit: "orange", count: 6},
Expand Down Expand Up @@ -350,6 +403,19 @@ iex> Enum.flat_map_reduce(cart, 0, fn item, acc ->
"orange", "orange", "orange", "orange", "orange"], 10}
```

### [Comprehension](`for/1`)

Flattening can also be done with comprehensions:

```elixir
iex> for item <- cart,
...> fruit <- List.duplicate(item.fruit, item.count) do
...> fruit
...> end
["apple", "apple", "apple", "banana", "orange",
"orange", "orange", "orange", "orange", "orange"]
```

## Conversion
{: .col-2}

Expand Down Expand Up @@ -377,6 +443,17 @@ iex> Enum.to_list(1..5)
[1, 2, 3, 4, 5]
```

### [Comprehension](`for/1`)

Conversion can also be done with comprehensions:

```elixir
iex> for item <- cart, into: %{} do
...> {item.fruit, item.count}
...> end
%{"apple" => 3, "banana" => 1, "orange" => 6}
```

## Duplicates & uniques
{: .col-2}

Expand Down Expand Up @@ -412,6 +489,8 @@ iex> Enum.uniq([1, 2, 2, 3, 3, 3, 1, 2, 3])
[1, 2, 3]
```

Comprehensions also support the `uniq: true` option.

### [uniq_by(enum, fun)](`Enum.uniq_by/2`)

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

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

### [zip_with(enum1, enum2, zip_fun)](`Enum.zip_with/2`)
### [zip_with(enum1, enum2, fun)](`Enum.zip_with/2`)

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

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

### [zip_reduce(left, right, acc, reducer)](`Enum.zip_reduce/4`)
### [zip_reduce(left, right, acc, fun)](`Enum.zip_reduce/4`)

```elixir
iex> fruits = ["apple", "banana", "orange"]
Expand Down

0 comments on commit 682b6d5

Please sign in to comment.