Class: Set

Inherits:
Object show all
Defined in:
lib/propr/random/set.rb,
lib/propr/shrink/set.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.random(options = {}, m = Propr::Random) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/propr/random/set.rb', line 10

def random(options = {}, m = Propr::Random)
  min  = options[:min] || 0
  max  = options[:max] || 10
  item = yield

  # @todo: Be sure we created enough *unique* elements
  m.bind(Integer.random(options.merge(min: min, max: max))) do |size|
    m.bind(m.sequence([item]*size)) do |xs|
      m.unit(xs.to_set)
    end
  end
end

Instance Method Details

#random(options = {}, m = Propr::Random) ⇒ Object



2
3
4
5
6
# File 'lib/propr/random/set.rb', line 2

def random(options = {}, m = Propr::Random)
  m.bind(m.rand(size)) do |index|
    m.unit(self.to_a[index])
  end
end

#shrinkArray<Set>

Returns:



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/propr/shrink/set.rb', line 3

def shrink
  return Array.new if empty?

  array = to_a
  array.combination(size - 1).map(&:to_set).tap do |shrunken|
    shrunken << Set.new

    size.times do |n|
      head = array[0, n]
      tail = array[n+1..-1]
      item = array[n]
      shrunken.concat(item.shrink.map{|m| (head + [m] + tail).to_set })
    end
  end
end