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


I think this is a good change; I just noticed it from the NEWS
file at:

https://github.com/ruby/ruby/blob/trunk/NEWS

I think of .select and .reject as filters already - we filter 
either for what we want to keep, or for what we want to discard.

At the least this is how my brain "remembers" this.

I tend to prefer .select, mostly because also how my brain works -
I find it easier to think in a "positive" way, e. g. which is why
I prefer to write code that uses "if condition" rather than 
"unless condition" - the second variant takes me a bit longer to
process, if I actively think about it.

Both .select and .reject act as filters ultimately, but since I 
myself try to write code in a way to prefer .select, it suits me
to see .filter being an alias to .select rather than an alias to
.reject.

I used to write code like this:

    Dir['**/**'].reject {|entry| File.directory?(entry) }

Like to get mostly files; but I think it works just fine via
.select too, and using a test via File.file? (and perhaps also
testing for symlinks).

Sorry for the long addition here - I only just noticed that change
just now when Benoit made the change recently. :)

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

* Author: davidarnold (David Arnold)
* Status: Closed
* Priority: Normal
* Assignee: Eregon (Benoit Daloze)
* Target version: 2.6
----------------------------------------
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>