Issue #5663 has been updated by Thomas Sawyer.


One of the problems with this method is that in becomes impossible to get `nil` or `false` as a valid selection/non-compaction.

Couple thoughts on handling this. For starters maybe #compact should be able to take an argument, e.g.

  [1,2,3,1].compact(1) => [2,3]

(This in itself seems pretty useful, regardless.)

Then a #compact_map could do the same. If we need to be able to get nil/false results we could then use an alternative.

  foo = Object.new

  [0,1,-1].compact_map(foo){ |e| e.zero? ? foo : (e > 0 ? true : false) } => [true,false]

(Here's a case where some sort of global `null` instance might be useful, in place of `foo`.)

But then again, a delayed enumeration is about as concise, and maybe a bit easier to read.

  [0,1,-1].denum.select{ |e| !e.zero? }.map{ |e| e > 0 ? true : false }.to_a

Something to consider.

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

Author: Yehuda Katz
Status: Assigned
Priority: Normal
Assignee: 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/