Add set.intersects method

Multiple times I wished that sets had an `intersects` method. Simply the negation of the `set.isdisjoint` method. Sometimes I can write `not a.isdisjoint(b)`, but: 1) Together with the "dis", that's a double negation. Saying "a.intersects(b)" is the correct and much clearer way to express my intent. 2) I sometimes want to pass a set's bound method, for example as the first argument to `filter`, and then I can't pass that `not` along with it. The isdisjoint method is great. Takes any iterable, returns a bool, and is efficient: It doesn't turn the iterable into a set like some other `set` methods, but just iterates it and checks each element against the set (and stops early if it finds a common value). I'd like the same for `set.intersects`.

On Thu, Dec 23, 2021 at 05:23:00PM -0000, Stefan Pochmann wrote:
Multiple times I wished that sets had an `intersects` method. Simply the negation of the `set.isdisjoint` method.
There are so many ways to get this: def intersects(a, b): return not a.isdisjoint(b) Not ever one-line function needs to be built-in. Or take advantage of the fact that empty sets are falsey and non-empty sets are truthy: if a.intersection(b): ... If you need lazy, bail-out early processing: if any(el in a for el in b): ... The problem is, there are so many easy, obvious ways to test for intersection, that it seems hardly worth the effort to add it to the set API. If we add it to set, we'd have to add it to frozenset, and the three dict views (keys, values, items); anyone who has their own custom set types would need to add it to their classes in order to keep the set API. That's not to say that we can't or won't add this method, but we have to way up the costs against the benefits. -- Steve

On Thu, Dec 23, 2021 at 05:23:00PM -0000, Stefan Pochmann wrote:
Multiple times I wished that sets had an `intersects` method. Simply the negation of the `set.isdisjoint` method.
There are so many ways to get this: def intersects(a, b): return not a.isdisjoint(b) Not ever one-line function needs to be built-in. Or take advantage of the fact that empty sets are falsey and non-empty sets are truthy: if a.intersection(b): ... If you need lazy, bail-out early processing: if any(el in a for el in b): ... The problem is, there are so many easy, obvious ways to test for intersection, that it seems hardly worth the effort to add it to the set API. If we add it to set, we'd have to add it to frozenset, and the three dict views (keys, values, items); anyone who has their own custom set types would need to add it to their classes in order to keep the set API. That's not to say that we can't or won't add this method, but we have to way up the costs against the benefits. -- Steve
participants (2)
-
Stefan Pochmann
-
Steven D'Aprano