Георги обнови решението на 27.10.2014 15:19 (преди около 10 години)
+module Operators
+ def &(other)
+ Filter.new { |x| self.filter.call x and other.filter.call x }
+ end
+
+ def |(other)
+ Filter.new { |x| self.filter.call x or other.filter.call x }
+ end
+end
+
+class NumberSet
+ include Enumerable
+
+ def initialize
+ @range = []
+ end
+
+ def each(&block)
+ @range.each &block
+ end
+
+ def <<(number)
+ @range << number unless @range.include?(number)
+ end
+
+ def size
+ @range.size
+ end
+
+ def empty?
+ @range.empty?
+ end
+
+ def [](argument)
+ new_range = NumberSet.new
+ @range.each do |number|
+ new_range << number if argument.filter.call number
+ end
+ new_range
+ end
+end
+
+class Filter
+ include Operators
+
+ def initialize(&block)
+ @condition_block = block
+ end
+
+ def filter
+ @condition_block
+ end
+end
+
+class TypeFilter
+ include Operators
+
+ def initialize(type)
+ @type = type
+ end
+
+ def filter
+ case @type
+ when :integer then -> (x) { x.is_a? Integer }
+ when :real then -> (x) { x.is_a? Float or x.is_a? Rational }
+ when :complex then -> (x) { x.is_a? Complex }
+ end
+ end
+end
+
+class SignFilter
+ include Operators
+ def initialize(sign)
+ @sign = sign
+ end
+
+ def filter
+ case @sign
+ when :positive then ->(x) { x > 0 }
+ when :non_positive then ->(x) { x <= 0 }
+ when :negative then ->(x) { x < 0 }
+ when :non_negative then ->(x) { x >= 0 }
+ end
+ end
+end