cjen07
Why Enum.filter_map is deprecated?
I just upgrade OTP to 20.0 and elixir to 1.5.0-rc.1, I got warnings on usage of filter_map. Why is it deprecated?
Most Liked
adamu
Hello from the future. The commit log doesn’t mention why it was deprecated, but whatyouhide, a core team member, explained a bit on this issue:
Elixir deprecated the usage of
Enum.filter_map/3because it was counterintuitive and the same could be achieved withEnum.filter/2+Enum.map/2, withStream, or with aforcomprehension.
And also José Valim in the mailing list:
I would use comprehensions:
for item <- 1, 2, 3, rem(item, 2) == 0, do: item * 2There is no reason for Enum.filter_map or Enum.map_filter to exist besides backwards compatibility.
kylethebaker
Here is an example of using comprehension in lieu of Enum.filter_map/3:
# filter even numbers
filter_fn = fn n ->
rem(n, 2) == 0
end
# double
map_fn = fn n ->
n * 2
end
some_enum = 1..20
# filter_map version
Enum.filter_map(some_enum, filter_fn, map_fn)
# comprehension version
for n <- some_enum, filter_fn.(n), do: map_fn.(n)
OvermindDL1
Technically flat_map, like reduce are considered base-most enumeration functions. flat_map is the base-most that always returns another enumeration, and reduce is the base-most that returns anything. filter_map is pretty duplicative as I always use flat_map anyways.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










