Issue #5663 has been updated by trans (Thomas Sawyer).


Instead of thinking of it as a special type of #map, I suggest thinking about it as a special type of #select. The reason is that we could also use the same type of behavior for #find. Which is why I suggest #select_yield and #find_yield (or #yield_select and #yield_find). The names of both of these need to be considered together.



----------------------------------------
Feature #5663: Combined map/select method
https://bugs.ruby-lang.org/issues/5663#change-28865

Author: wycats (Yehuda Katz)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: lib
Target version: 2.0.0


It is pretty common to want to map over an Enumerable, but only include the elements that match a particular filter. A common idiom is:

enum.map { |i| i + 1 if i.even? }.compact

It is of course also possible to do this with two calls:

enum.select { |i| i.even? }.map { |i| i + 1 }

Both cases are clumsy and require two iterations through the loop. I'd like to propose a combined method:

enum.map_select { |i| i + 1 if i.even? }

The only caveat is that it would be impossible to intentionally return nil here; suggestions welcome. The naming is also a strawman; feel free to propose something better.


-- 
http://bugs.ruby-lang.org/