Alternatively we can change return value to always be tuple in form
`{boolean(), any()}` where first value describe value of the `filter` and
second is mapping.
Enum.filter_map([1,2,3], fn item ->
{item % 2 == 0, item * 2}
end) # => [4]
However I find such form a little less readable and more error prone.
W dniu środa, 26 kwietnia 2017 12:42:11 UTC+2 użytkownik Łukasz Niemier
napisał:
>
> In current form `Enum.filter_map/3` isn't always suitable as sometimes we
> want to first filter then map and sometimes other way around. This is where
> my proposal comes in that will allow both of these behaviours be available
> at almost no cost in the same method:
>
> Enum.filter_map([1,2,3], fn item ->
> if item % 2 == 0 do
> {:keep, item * 2}
> else
> :ignore
> end
> end)
>
> This form is IMHO more readable, as it explicitly states the order of
> filter and map. This design is based on Rust's
> std::tier::Iterator::filter_map
> <https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.filter_map>,
> however as Elixir doesn't have sum types I needed to use some form of
> tuple/atom workaround.
>
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/774e8354-4b7c-47f2-a3a9-77aebce3a60f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.