True but personally don’t always think !any is clearer. And sometimes you have a long chain of iterator methods so adding ! way at the start is confusing.
I didn't know about .not(). That does make things a bit more ergonomic I guess. I still don't think the readability is as good though.
I also don't think that just because you can combine two existing methods doesn't mean we shouldn't add this as a convenience. We have .find_map(f) which is just .filter_map(f).next()
Personally I like Ruby's style of offering logical duals consistently. any has none, if has unless. Yes, one is the negation of the other, and there are a variety of ways to write code that does the same thing (negation, importing the Not trait to use .not() [although I think most folks may not be aware this is possible]), but I personally think writing none is clearer (and less likely to do wrong) than the alternatives.
No, this is incorrect. iter.all(f).not() should return true if the iterator may have some elements that pass the test, but not all of the elements pass the test.
Just as another data point of prior art, the C++ STL also has std::all_of, std::any_of and std::none_of.
I assume that the fourth variant was deemed to rare to deserve its own function.
Alright, here's an updated Playground example covering all the cases. Functions already provided are commented out. Two of the functions have an added Copy bound which would be avoided in a real-world implementation, but which is placed here to simplify the example: Playground example
let v = vec![2, 1];
let r = v.iter().any_not_all(is_odd);
assert_eq!(r, true); // panics
let v = vec![1, 2];
let r = v.iter().all_or_none(is_odd);
assert_eq!(r, false); // panics
That's because the any and all methods consume items from the iterator, so when one returns early, the other only checks the remainder of the elements.
Normally that would be an argument in favour of inclusion, but… how often does one need all_or_none or any_not_all, really? I don't see any good use case for it. And the others can already be succinctly expressed in terms of !, or if you insist, Borat conditions. They aren't any more expressive, clear or readable than that, and I'd argue they are less: I'd be less confident reading code that freely interchanges those combinators, because I'd have to memorise them all to be sure they perform as they are named (hello, #define FIVE 42). If they aren't there, not only there'd be less to memorise, but the logical structure of the condition would be much more apparent, making it easier to notice how code could be refactored.