cjen07

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

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/3 because it was counterintuitive and the same could be achieved with Enum.filter/2 + Enum.map/2, with Stream, or with a for comprehension.

And also José Valim in the mailing list:

I would use comprehensions:

for item <- 1, 2, 3,
    rem(item, 2) == 0,
    do: item * 2

There is no reason for Enum.filter_map or Enum.map_filter to exist besides backwards compatibility.

kylethebaker

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

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.

michalmuskala

michalmuskala

I think the reason was the fact that it was an outlier. The only “fused” function.

gregvaughn

gregvaughn

Parsing the English can be odd, but they’re these things, covered in the Guide https://elixir-lang.org/getting-started/comprehensions.html

for comprehensions consist of generators and filters, though the filters seem to be less widely known

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement