Issue #13784 has been updated by adp90 (Alexander Patrick).


davidarnold (David Arnold) wrote:
> For the purpose of this feature request, I would leave the filter == find_all behavior the same.  If the decision in the bug report is that Hash#find_all should match Hash#select, then an alias can be added for both Hash#find_all and Hash#filter.

I decided to update the pull request so that 'filter' and 'filter!' would work like 'select' and 'select!' for Array, Hash, Set, SortedSet, and ENV. I added spec and tests for filter to each of these, and given find_all's strange lack of testing or documentation, basing it on select seemed like the best course of action.

'Find_all' only functions differently than 'select' for Hashes, but has no spec or tests outside of Enumerable, while select is well-documented and tested throughout Ruby. Given that it's an alias for select, it's also curious that there is no 'find_all!'

----------------------------------------
Feature #13784: Add Enumerable#filter as an alias of Enumerable#select
https://bugs.ruby-lang.org/issues/13784#change-66077

* Author: davidarnold (David Arnold)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Ruby has a full set of functional tools in the Enumerable module under the "-ect" methods (viz. collect, select, inject).  However the usual industry terms for these are map, filter, and reduce.

For example, Swift, Python, and ECMAScript all use the names map, filter, and reduce to describe these methods.  Also, this language independent MIT course uses map, filter and reduce: http://web.mit.edu/6.005/www/fa15/classes/25-map-filter-reduce/

Ruby has aliases for map and reduce, but filter is noticeably absent.  This feature request is simply to add an alias to Enumerable for filter.  This will ease the transition of developers from other languages to Ruby.

Desired behavior:

[:foo, :bar].filter { |x| x == :foo } # => [:foo]

Current behavior:

[:foo, :bar].filter { |x| x == :foo } # NoMethodError: undefined method `filter'



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