Module: Cantor

Defined in:
lib/cantor.rb,
lib/cantor/null_set.rb,
lib/cantor/absolute_set.rb,
lib/cantor/abstract_set.rb,
lib/cantor/relative_set.rb,
lib/cantor/universal_set.rb,
lib/cantor/relative_complement.rb

Defined Under Namespace

Classes: AbsoluteSet, AbstractSet, RelativeComplement, RelativeSet

Constant Summary collapse

NullSet =
Class.new(RelativeSet) do

  def initialize
  end

  # @return [NullSet]
  def build
    self
  end

  # Yields each element in the set to the implicit block argument. Since
  # there are no elements, the block is never executed
  #
  # @return [void]
  def each
  end

  # Returns an {Array} containing each element in this set. Since there are
  # no elements, the {Array} is empty
  #
  # @return [Array]
  def to_a
    []
  end

  # @return false
  def include?(other)
    false
  end

  # @return true
  def finite?
    true
  end

  # @return [Set] other
  def replace(other)
    Cantor.build(other)
  end

  # @return 0
  def size
    0
  end

  # @return true
  def empty?
    true
  end

  # @group Set Operations
  #########################################################################

  # @return self
  def map
    self
  end

  # @return self
  def select
    self
  end

  # @return self
  def reject
    self
  end

  # @return UniversalSet
  def complement
    # ¬∅ = U
    UniversalSet.build
  end

  # @return self
  def intersection(other)
    # ∅ ∩ A = ∅
    self
  end

  # @return [Set] other
  def union(other)
    # ∅ ∪ A = A
    Cantor.build(other)
  end

  # @return self
  def difference(other)
    # ∅ ∖ A = A
    self
  end

  # @return [Set] other
  def symmetric_difference(other)
    # ∅ ⊖ A = A
    Cantor.build(other)
  end

  # @group Set Ordering
  #########################################################################

  def ==(other)
    eql?(other) or other.empty?
  end

  # @group Pretty Printing
  #########################################################################

  # @return [String]
  def inspect
    "NullSet"
  end

  # @endgroup
  #########################################################################
end.new
UniversalSet =
Class.new(RelativeComplement) do

  def initialize
  end

  # @return [UniversalSet]
  def build
    self
  end

  # @return true
  def include?(object)
    true
  end

  # @return false
  def finite?
    false
  end

  # @return Infinity
  def size
    1.0 / 0.0
  end

  # @return false
  def empty?
    false
  end

  # @return [AbstractSet]
  def replace(other)
    Cantor.build(other)
  end

  # @group Set Operations
  #########################################################################

  # @return NullSet
  def complement
    # ¬U = ∅
    NullSet.build
  end

  # @return [AbstractSet]
  def intersection(other)
    # U & A = A
    Cantor.build(other)
  end

  # @return [AbstractSet]
  def union(other)
    # U ∪ A = U
    self
  end

  # @return [AbstractSet]
  def symmetric_difference(other)
    # U ⊖ A = (U ∖ A) ∪ (A ∖ U) = (¬A) ∪ (∅) = ¬A
    Cantor.complement(other)
  end

  # @return [AbstractSet]
  def difference(other)
    # U ∖ A = ¬A
    Cantor.complement(other)
  end

  # @group Set Ordering
  #########################################################################

  # @return [Boolean]
  def proper_subset?(other)
    false
  end

  # @return [Boolean]
  def proper_superset?(other)
    not other.eql?(self)
  end

  # @return [Boolean]
  def ==(other)
    eql?(other)
  end

  # @group Pretty Printing
  #########################################################################

  # @return [String]
  def inspect
    "UniversalSet"
  end

  # @endgroup
  #########################################################################
end.new

Constructors collapse

Class Method Details

.absolute(other, universe = other) ⇒ Cantor::AbsoluteSet

Returns:



42
43
44
45
46
47
48
49
50
# File 'lib/cantor.rb', line 42

def absolute(other, universe = other)
  if universe == Cantor::UniversalSet
    build(other)
  elsif universe.eql?(other)
    Cantor::AbsoluteSet.build(universe)
  else
    Cantor::AbsoluteSet.build(universe).intersection(other)
  end
end

.build(object) ⇒ Cantor::AbstractSet

Returns:



15
16
17
18
19
20
21
22
23
24
# File 'lib/cantor.rb', line 15

def build(object)
  if object.is_a?(Cantor::AbstractSet)
    object
  elsif object.is_a?(Enumerable)
    Cantor::RelativeSet.build(object)
  else
    raise TypeError,
      "argument must be an AbstractSet or Enumerable"
  end
end

.complement(other) ⇒ Cantor::AbstractSet

Returns:



27
28
29
# File 'lib/cantor.rb', line 27

def complement(other)
  build(other).complement
end

.emptyCantor::NullSet

Returns:



37
38
39
# File 'lib/cantor.rb', line 37

def empty
  Cantor::NullSet.build
end

.universalCantor::UniversalSet



32
33
34
# File 'lib/cantor.rb', line 32

def universal
  Cantor::UniversalSet.build
end