Forum: Ruby-core [ruby-trunk - Feature #6588][Open] Set#intersect?

0e610136db92027148906c92d57fdb36?d=identicon&s=25 marcandre (Marc-Andre Lafortune) (Guest)
on 2012-06-14 04:47
(Received via mailing list)
Issue #6588 has been reported by marcandre (Marc-Andre Lafortune).

----------------------------------------
Feature #6588: Set#intersect?
https://bugs.ruby-lang.org/issues/6588

Author: marcandre (Marc-Andre Lafortune)
Status: Open
Priority: Normal
Assignee: knu (Akinori MUSHA)
Category: lib
Target version: 2.0.0


There is `Set#superset?`, `Set#subset?` with their `proper` variants,
but there is no `Set#intersect?` nor `Set#disjoint?`

To check if two sets intersect, one can do

  set.intersection(other).empty?

This cycles through all elements, though. To be efficient, one can
instead do the iteration manually:

  other.any? { |x| set.include?(x) }

I think it would be natural to add `Set#intersect?` and its reverse
`Set#disjoint?`

  class Set
    def intersect?(enum)
      enum.any? { |x| include?(x) }
    end
  end

Maybe it would be worth it to optimize it if enum is a larger Set by
starting it with

      return any? { |x| enum.include?(x) } if enum.is_a?(Set) &&
enum.size > size
F24ff61beb80aa5f13371aa22a35619c?d=identicon&s=25 mame (Yusuke Endoh) (Guest)
on 2012-07-14 11:39
(Received via mailing list)
Issue #6588 has been updated by mame (Yusuke Endoh).

Status changed from Open to Assigned


----------------------------------------
Feature #6588: Set#intersect?
https://bugs.ruby-lang.org/issues/6588#change-28085

Author: marcandre (Marc-Andre Lafortune)
Status: Assigned
Priority: Normal
Assignee: knu (Akinori MUSHA)
Category: lib
Target version: 2.0.0


There is `Set#superset?`, `Set#subset?` with their `proper` variants,
but there is no `Set#intersect?` nor `Set#disjoint?`

To check if two sets intersect, one can do

  set.intersection(other).empty?

This cycles through all elements, though. To be efficient, one can
instead do the iteration manually:

  other.any? { |x| set.include?(x) }

I think it would be natural to add `Set#intersect?` and its reverse
`Set#disjoint?`

  class Set
    def intersect?(enum)
      enum.any? { |x| include?(x) }
    end
  end

Maybe it would be worth it to optimize it if enum is a larger Set by
starting it with

      return any? { |x| enum.include?(x) } if enum.is_a?(Set) &&
enum.size > size
C4e88907313843cf07f6d85ba8162120?d=identicon&s=25 yhara (Yutaka HARA) (Guest)
on 2012-10-25 12:45
(Received via mailing list)
Issue #6588 has been updated by yhara (Yutaka HARA).

Target version changed from 2.0.0 to next minor


----------------------------------------
Feature #6588: Set#intersect?
https://bugs.ruby-lang.org/issues/6588#change-31537

Author: marcandre (Marc-Andre Lafortune)
Status: Assigned
Priority: Normal
Assignee: knu (Akinori MUSHA)
Category: lib
Target version: next minor


There is `Set#superset?`, `Set#subset?` with their `proper` variants,
but there is no `Set#intersect?` nor `Set#disjoint?`

To check if two sets intersect, one can do

  set.intersection(other).empty?

This cycles through all elements, though. To be efficient, one can
instead do the iteration manually:

  other.any? { |x| set.include?(x) }

I think it would be natural to add `Set#intersect?` and its reverse
`Set#disjoint?`

  class Set
    def intersect?(enum)
      enum.any? { |x| include?(x) }
    end
  end

Maybe it would be worth it to optimize it if enum is a larger Set by
starting it with

      return any? { |x| enum.include?(x) } if enum.is_a?(Set) &&
enum.size > size
0e610136db92027148906c92d57fdb36?d=identicon&s=25 marcandre (Marc-Andre Lafortune) (Guest)
on 2012-11-12 05:13
(Received via mailing list)
Issue #6588 has been updated by marcandre (Marc-Andre Lafortune).


Comment about these simple features would be appreciated.

----------------------------------------
Feature #6588: Set#intersect?
https://bugs.ruby-lang.org/issues/6588#change-32800

Author: marcandre (Marc-Andre Lafortune)
Status: Assigned
Priority: Normal
Assignee: knu (Akinori MUSHA)
Category: lib
Target version: next minor


There is `Set#superset?`, `Set#subset?` with their `proper` variants,
but there is no `Set#intersect?` nor `Set#disjoint?`

To check if two sets intersect, one can do

  set.intersection(other).empty?

This cycles through all elements, though. To be efficient, one can
instead do the iteration manually:

  other.any? { |x| set.include?(x) }

I think it would be natural to add `Set#intersect?` and its reverse
`Set#disjoint?`

  class Set
    def intersect?(enum)
      enum.any? { |x| include?(x) }
    end
  end

Maybe it would be worth it to optimize it if enum is a larger Set by
starting it with

      return any? { |x| enum.include?(x) } if enum.is_a?(Set) &&
enum.size > size
C4e88907313843cf07f6d85ba8162120?d=identicon&s=25 alexeymuranov (Alexey Muranov) (Guest)
on 2012-11-12 10:09
(Received via mailing list)
Issue #6588 has been updated by alexeymuranov (Alexey Muranov).


+1.  Maybe `#meet?` instead of `#intersect?` ? It can be argued that any
set intersects any other, just the intersection is sometimes empty :).
----------------------------------------
Feature #6588: Set#intersect?
https://bugs.ruby-lang.org/issues/6588#change-32813

Author: marcandre (Marc-Andre Lafortune)
Status: Assigned
Priority: Normal
Assignee: knu (Akinori MUSHA)
Category: lib
Target version: next minor


There is `Set#superset?`, `Set#subset?` with their `proper` variants,
but there is no `Set#intersect?` nor `Set#disjoint?`

To check if two sets intersect, one can do

  set.intersection(other).empty?

This cycles through all elements, though. To be efficient, one can
instead do the iteration manually:

  other.any? { |x| set.include?(x) }

I think it would be natural to add `Set#intersect?` and its reverse
`Set#disjoint?`

  class Set
    def intersect?(enum)
      enum.any? { |x| include?(x) }
    end
  end

Maybe it would be worth it to optimize it if enum is a larger Set by
starting it with

      return any? { |x| enum.include?(x) } if enum.is_a?(Set) &&
enum.size > size
0e610136db92027148906c92d57fdb36?d=identicon&s=25 marcandre (Marc-Andre Lafortune) (Guest)
on 2012-11-12 16:38
(Received via mailing list)
Issue #6588 has been updated by marcandre (Marc-Andre Lafortune).


alexeymuranov (Alexey Muranov) wrote:
> +1.

Thanks for the +1

> It can be argued that any set intersects any other, just the intersection is
sometimes empty :).

No, I believe it would be wrong to argue that. From wikipedia: "We say
that A intersects B if A intersects B at some element"

Moreover: "We say that A and B are disjoint if A does not intersect B.
In plain language, they have no elements in common"

I believe that both `intersect?` and `disjoint?` are the established
terms for the concept I'm proposing.

----------------------------------------
Feature #6588: Set#intersect?
https://bugs.ruby-lang.org/issues/6588#change-32818

Author: marcandre (Marc-Andre Lafortune)
Status: Assigned
Priority: Normal
Assignee: knu (Akinori MUSHA)
Category: lib
Target version: next minor


There is `Set#superset?`, `Set#subset?` with their `proper` variants,
but there is no `Set#intersect?` nor `Set#disjoint?`

To check if two sets intersect, one can do

  set.intersection(other).empty?

This cycles through all elements, though. To be efficient, one can
instead do the iteration manually:

  other.any? { |x| set.include?(x) }

I think it would be natural to add `Set#intersect?` and its reverse
`Set#disjoint?`

  class Set
    def intersect?(enum)
      enum.any? { |x| include?(x) }
    end
  end

Maybe it would be worth it to optimize it if enum is a larger Set by
starting it with

      return any? { |x| enum.include?(x) } if enum.is_a?(Set) &&
enum.size > size
6789224081b49822eb70f6740beb5ed5?d=identicon&s=25 knu (Akinori MUSHA) (Guest)
on 2013-07-27 08:00
(Received via mailing list)
Issue #6588 has been updated by knu (Akinori MUSHA).


OK, accepted.  I'll work on it.
----------------------------------------
Feature #6588: Set#intersect?
https://bugs.ruby-lang.org/issues/6588#change-40705

Author: marcandre (Marc-Andre Lafortune)
Status: Assigned
Priority: Normal
Assignee: knu (Akinori MUSHA)
Category: lib
Target version: next minor


There is `Set#superset?`, `Set#subset?` with their `proper` variants,
but there is no `Set#intersect?` nor `Set#disjoint?`

To check if two sets intersect, one can do

  set.intersection(other).empty?

This cycles through all elements, though. To be efficient, one can
instead do the iteration manually:

  other.any? { |x| set.include?(x) }

I think it would be natural to add `Set#intersect?` and its reverse
`Set#disjoint?`

  class Set
    def intersect?(enum)
      enum.any? { |x| include?(x) }
    end
  end

Maybe it would be worth it to optimize it if enum is a larger Set by
starting it with

      return any? { |x| enum.include?(x) } if enum.is_a?(Set) &&
enum.size > size
6789224081b49822eb70f6740beb5ed5?d=identicon&s=25 knu (Akinori MUSHA) (Guest)
on 2013-07-30 12:20
(Received via mailing list)
Issue #6588 has been updated by knu (Akinori MUSHA).


I followed superset?() and the like, and made the new methods accept
only a set for the moment, because I couldn't come up with an idea of
how to deal with Range.  For example:

- if Set[2].intersect?(1.5..2.5) should return true
- if Set[2].intersect?(3..(1.0/0)) should immediately return false using
some knowledge on Range

----------------------------------------
Feature #6588: Set#intersect?
https://bugs.ruby-lang.org/issues/6588#change-40759

Author: marcandre (Marc-Andre Lafortune)
Status: Closed
Priority: Normal
Assignee: knu (Akinori MUSHA)
Category: lib
Target version: next minor


There is `Set#superset?`, `Set#subset?` with their `proper` variants,
but there is no `Set#intersect?` nor `Set#disjoint?`

To check if two sets intersect, one can do

  set.intersection(other).empty?

This cycles through all elements, though. To be efficient, one can
instead do the iteration manually:

  other.any? { |x| set.include?(x) }

I think it would be natural to add `Set#intersect?` and its reverse
`Set#disjoint?`

  class Set
    def intersect?(enum)
      enum.any? { |x| include?(x) }
    end
  end

Maybe it would be worth it to optimize it if enum is a larger Set by
starting it with

      return any? { |x| enum.include?(x) } if enum.is_a?(Set) &&
enum.size > size
This topic is locked and can not be replied to.