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?
Changelog for 1.5 says:
[Enum] Deprecate Enum.filter_map/3 in favor of Enum.filter/2 + Enum.map/2 or for-comprehensions
Yes, ‘filter+map’ or ‘for’ works, but I want to know the reason behind it.
I think the reason was the fact that it was an outlier. The only “fused” function.
Can someone help a rookie (me) understand the
[…] or for comprehensions
component of this deprecation message?
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
Here is an example of using comprehension in lieu of
# 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)
map_join and the rest of the “fused” function?
Most of them are useful as a way to avoid walking a long list twice.
If you fear multi walks, then use the appropriate
Enum functions is just a legacy implementation? They don’t give us anything extra, they just help us avoid some extra code for some common use-cases?
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