Skip to content

Commit

Permalink
Add a note about chaining multiple map+filter calls
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Dec 14, 2021
1 parent 7e66a4c commit 8804ff5
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions lib/elixir/lib/map.ex
Original file line number Diff line number Diff line change
Expand Up @@ -979,12 +979,14 @@ defmodule Map do
`fun` receives the key and value of each of the
elements in the map as a key-value pair.
`Map.filter/2` is faster than using `map |> Enum.filter(fun) |> Enum.into(%{})`,
as no intermediate list is being built.
See also `reject/2` which discards all elements where the
function returns a truthy value.
> Note: if you find yourself doing multiple calls to `Map.map/2`
> and `Map.filter/2` in a pipeline, it is likely more efficient
> to use `Enum.map/2` and `Enum.filter/2` instead and convert to
> a map at the end using `Map.new/1`.
## Examples
iex> Map.filter(%{one: 1, two: 2, three: 3}, fn {_key, val} -> rem(val, 2) == 1 end)
Expand All @@ -1010,9 +1012,8 @@ defmodule Map do
end

@doc """
Returns map excluding the pairs from `map` for which `fun` returns a truthy value.
`Map.reject/2` is faster than using `map |> Enum.reject(fun) |> Enum.into(%{})`,
as no intermediate list is being built.
Returns map excluding the pairs from `map` for which `fun` returns
a truthy value.
See also `filter/2`.
Expand Down Expand Up @@ -1041,8 +1042,15 @@ defmodule Map do
end

@doc """
Maps the function `fun` over all key-value pairs in `map`, returning a map
with all the values replaced with the result of the function.
Maps the function `fun` over all key-value pairs in `map`
It returns a map with all the values replaced with the result
of the function.
> Note: if you find yourself doing multiple calls to `Map.map/2`
> and `Map.filter/2` in a pipeline, it is likely more efficient
> to use `Enum.map/2` and `Enum.filter/2` instead and convert to
> a map at the end using `Map.new/1`.
## Examples
Expand Down

0 comments on commit 8804ff5

Please sign in to comment.