Skip to content

Instantly share code, notes, and snippets.

@denisdefreyne
Last active December 24, 2015 05:39
Show Gist options
  • Save denisdefreyne/6752017 to your computer and use it in GitHub Desktop.
Save denisdefreyne/6752017 to your computer and use it in GitHub Desktop.
Experimental implementation of special subsets with + operator.
require 'set'
class FullSubset
def +(other)
self
end
def inspect
"<FullSubset>"
end
end
class NonFullSubset
attr_reader :elements
def initialize(elements)
@elements = Set.new(elements)
end
def +(other)
case other
when NonFullSubset
NonFullSubset.new(self.elements + other.elements)
else
other + self
end
end
def inspect
"<NonFullSubset(#{@elements.to_a.sort.join(', ')})>"
end
end
###############################################################################
a = NonFullSubset.new([ 1, 2, 3 ])
b = NonFullSubset.new([ 3, 4, 5 ])
full = FullSubset.new
empty = NonFullSubset.new([])
p a
p b
p full
p empty
puts
puts '- - - - -'
puts
p a+a
p a+b
p a+full
p a+empty
puts
p b+a
p b+b
p b+full
p b+empty
puts
p full+a
p full+b
p full+full
p full+empty
puts
p empty+a
p empty+b
p empty+full
p empty+empty
puts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment