I tend to use Enum.flat_map/2 for that and return [element] instead of {:keep, 
_} and [] instead of :ignore. It may be a bit slower because of the flattening, 
but it's never been the bottleneck (or even noticeable) in any code I wrote.

On Apr 26, 2017, 12:42 PM +0200, Łukasz Niemier <[email protected]>, wrote:
> 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, 
> 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/053143ce-efec-44f3-8e44-45bc36b90a67%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
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/45c404e8-fe92-4c58-9bce-3c49a2f67749%40Spark.
For more options, visit https://groups.google.com/d/optout.

Reply via email to