From: shevegen@...
Date: 2018-11-20T17:07:19+00:00
Subject: [ruby-core:89912] [Ruby trunk Feature#15323] [PATCH] Proposal: Add Enumerable#filter_map

Issue #15323 has been updated by shevegen (Robert A. Heiler).


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
methods.

.reduce() has, in my opinion, a slight other disadvantage, and that
is that people have to explicitely pass an [] (as in the example
here), 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)

----------------------------------------
Feature #15323: [PATCH] Proposal: Add Enumerable#filter_map
https://bugs.ruby-lang.org/issues/15323#change-74993

* Author: alfonsojimenez (Alfonso Jim��nez)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
This is a proposal for a combined `filter` + `map` method (https://bugs.ruby-lang.org/issues/5663).

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

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

GitHub PR: https://github.com/ruby/ruby/pull/2017


 

---Files--------------------------------
0001-Adding-Enumerable-filter_map.patch (4.61 KB)


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

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>