diff --git a/enum.cheatmd b/enum.cheatmd index 37d1bee..4b08ceb 100644 --- a/enum.cheatmd +++ b/enum.cheatmd @@ -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 @@ -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`