Skip to content

Commit

Permalink
Mention flat_map/2 in filtering cheatsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
sabiwara committed Aug 26, 2024
1 parent 4154b37 commit 1b10791
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion enum.cheatmd
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ iex> Enum.split_with(["ant", "bat", "cat"], & &1 =~ "at")
{["bat", "cat"], ["ant"]}
```

#### Filtering + transforming in one pass: `for/1` comprehension
#### Filtering + transforming in one pass:

Using a `for/1` comprehension:

```elixir
iex> for s <- ["ant", "bat", "cat"], s =~ "at" do
Expand All @@ -144,6 +146,15 @@ iex> for s <- ["ant", "bat", "cat"], s =~ "at" do
["Bat", "Cat"]
```

Using `Enum.flat_map/2`:

```elixir
iex> Enum.flat_map(["ant", "bat", "cat"], fn s ->
...> if s =~ "at", do: [String.capitalize(s)], else: []
...> end)
["Bat", "Cat"]
```

### Slicing

#### From a range: `Enum.slice/2`
Expand Down

0 comments on commit 1b10791

Please sign in to comment.