Feature #15323

[PATCH] Proposal: Add Enumerable#filter_map

Added by alfonsojimenez (Alfonso Jiménez) 4 months ago. Updated about 1 month ago.

Target version:


This is a proposal for a combined filter + map method (

This method both filters and maps the elements of an enumerable in just one iteration:

(1..10).filter_map { |i| i * 2 if i.even? } #=> [4, 8, 12, 16, 20]

GitHub PR:


0001-Adding-Enumerable-filter_map.patch (4.61 KB) 0001-Adding-Enumerable-filter_map.patch alfonsojimenez (Alfonso Jiménez), 11/20/2018 02:24 PM

Related issues

Related to Ruby trunk - Feature #5663: Combined map/select methodAssignedActions



Updated by alfonsojimenez (Alfonso Jiménez) 4 months ago

  • Description updated (diff)

Updated by alfonsojimenez (Alfonso Jiménez) 4 months ago

  • File deleted (0001-Adding-Enumerable-filter_map.patch)

Updated by tny (Tony Sunny) 4 months ago

Could't we use reduce for this?

(1..10).reduce([]) { |a, i| i.even? ? a << (i * 2) : a }

Updated by shevegen (Robert A. Heiler) 4 months ago

I think the functionality, that is to combine .filter (be it select
or reject, is secondary to me), and .map, could be useful. I don't
really need it myself but I find it is not entirely out of the question
that others may find it useful.

There is, IMO, only one real drawback, if we ignore the functionality
aspect (where you'd have to ask matz anyway), and this is that I
think the two-word methods can be quite clumsy.

Not just .filter_map but also .yield_self, which eventually had an
alias called .then. If we ignore the question as to whether .then
is a good name (or .yield_self), one advantage that .then has
is that it is shorter.

Succinct expression is not always necessarily the best; but in this
case, I think single-word methods are very often better than two-word

.reduce() has, in my opinion, a slight other disadvantage, and that
is that people have to explicitely pass an , which is not always easy to remember. (For me it is hard
to remember because I rarely use .reduce either).

This is just my opinion, though. I do not really have any strong pro
or con way about the feature itself; only a very tiny dislike of
.filter_map as name. But it is not really a strong contra opinion
either way. (My biggest look ahead is on ruby's jit/mjit ... :D)

Updated by devpolish (Nardo Nykolyszyn) 3 months ago

(1..10).map { |e| e.even? ? (e * 2) : e }

Updated by oleynikov (Alexander Oleynikov) 3 months ago wrote:

(1..10).map { |e| e.even? ? (e * 2) : e }

Yeah, but without #filter this is still an array with 10 elements.

Updated by phluid61 (Matthew Kerwin) 3 months ago

tny (Tony Sunny) wrote:

Could't we use reduce for this?

(1..10).reduce([]) { |a, i| i.even? ? a << (i * 2) : a }

Yep, that's mentioned in the original ticket too. There's also #each_with_object that lets you write the block almost the same as in the proposal:

(1..10).each_with_object([]) { |i, a| a << i * 2 if i.even? }

The big difference here is you can capture nil/false values, because the filter test is explicitly separated from the map operation.


Updated by shugo (Shugo Maeda) about 1 month ago

Updated by shugo (Shugo Maeda) about 1 month ago

+1 for filter_map.

Matz agreed the feature itself before:
The name filter_map is good because other languages have similar names (e.g., filter-map in Scheme).

Also available in: Atom PDF