Project

General

Profile

Feature #5663

Updated by nobu (Nobuyoshi Nakada) 4 months ago

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

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

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

 ```ruby 
 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: 

 ```ruby 
 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.

Back