Feature #5644
closedadd Enumerable#exclude? antonym
Added by sunaku (Suraj Kurapati) about 13 years ago. Updated almost 7 years ago.
Description
Please add Enumerable#exclude? as antonym of Enumerable#include?
This allows me to construct Boolean expressions more pleasantly:
if File.exist? some_file and not some_list.include? some_file
Can be written as:
if File.exist? some_file and some_list.exclude? some_file
Thanks for your consideration.
Updated by trans (Thomas Sawyer) about 13 years ago
Hey, a use for functors!
module Kernel
def not
Functor.new do |op,*a,&b|
!send(op,*a,&b)
end
end
end
some_list.not.include? some_file
Nothing against Enumerable#exclude? though. Seems reasonable.
Updated by mame (Yusuke Endoh) over 12 years ago
- Status changed from Open to Assigned
- Assignee set to matz (Yukihiro Matsumoto)
Updated by matz (Yukihiro Matsumoto) over 12 years ago
- Status changed from Assigned to Feedback
from that logic, don't we need to add antonyms to every predicate methods?
could you show us why include? is special?
Matz.
Updated by sunaku (Suraj Kurapati) over 12 years ago
Hi Matz,
I didn't ask for antonmys for all predicates; only for #exclude?.
The reason for #exclude? is for more "natural" boolean expressions:
if File.exist? some_file and some_list.exclude? some_file
if File.exist? some_file and not some_list.include? some_file
That "not SOMETHING.include? SOMETHING" pattern appears often in my code, so that's why I created this request. Of course, me saying "appears often" does not constitute as solid evidence in support of adding #exclude? so I have no choice but to accept your judgement on this request. It's your call.
Thanks for your consideration.
Updated by trans (Thomas Sawyer) over 12 years ago
I'll throw my hat in with #exclude? too. There's been a number of times that I would have liked to have it. "not include?" is a rather common predication and it's nice when code can line up neatly.
Updated by now (Nikolai Weibull) over 12 years ago
On Thu, Mar 29, 2012 at 01:26, sunaku (Suraj Kurapati) sunaku@gmail.com wrote:
Issue #5644 has been updated by sunaku (Suraj Kurapati).
The reason for #exclude? is for more "natural" boolean expressions
I don’t think #exclude? really conveys what’s being done very well.
Yes, “exclude” is the antonym of “include”, but the meaning of %[a b
c].include? x is very natural, whereas %[a b c].exclude? x isn’t.
Does it mean that %[a b c] contains the elements that should be
excluded and is x among them, or is x not included among the elements
of %[a b c]?
Updated by matz (Yukihiro Matsumoto) over 12 years ago
OK, you think negative for include? is special. Understood.
But as Nikolai pointed out, exclude? is not the best name for the function.
Any alternative?
Matz.
Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 12 years ago
At first I agreed with Nikolay, but then I changed my minded because the method is called "exclude?" with a question mark, not "exclude", so I don't think anyone would expect that it would actually remove some element.
Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 12 years ago
I think I've misunderstood the question posed by Nikolai. I've just read it again but I think that the other meaning presented by him doesn't make any sense. "does the array contain the elements that should be excluded?". Really? I read this like in English:
Does [1, 3, 5] exclude 4?
Ask someone that doesn't know anything about programming and see what will she answer to this question.
Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 12 years ago
The most common antonym is "exclude", but maybe we could use "omit" if you prefer:
Updated by trans (Thomas Sawyer) over 12 years ago
There really is no better term b/c all such terms are going to have the same connotations.
As with "include" if you add an "s" to the word then it reads more like typical English, i.e. "a excludes b ?". To use the singular form you have to add a modal verb like "does a exclude b ?" Which makes it easy to see that this is the right meaning.
Updated by now (Nikolai Weibull) over 12 years ago
On Fri, Mar 30, 2012 at 16:02, matz (Yukihiro Matsumoto)
matz@ruby-lang.org wrote:
OK, you think negative for include? is special. Understood.
But as Nikolai pointed out, exclude? is not the best name for the function.
Any alternative?
How about changing the definition of Enumerable#none?, #any?, and
#all? to take an optional argument that is compared against each
element using #==. Then we have
if File.exist? some_file and some_list.none? some_file
I would not write it like that – I’d still use not a.include? b, which
I think is hard to beat – but then we at least don’t need to come up
with a new name and these three methods gain semantics that I’ve felt
they were lacking.
Updated by mame (Yusuke Endoh) about 12 years ago
- Target version set to 2.6
Updated by trans (Thomas Sawyer) about 12 years ago
Too bad we can't use symbols like ∉
.