Class: Properb::ShrinkTree

Inherits:
Object
  • Object
show all
Defined in:
lib/properb/shrink_tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, children) ⇒ ShrinkTree



5
6
7
8
# File 'lib/properb/shrink_tree.rb', line 5

def initialize(value, children)
  @value = value
  @children = children.lazy
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



3
4
5
# File 'lib/properb/shrink_tree.rb', line 3

def children
  @children
end

#valueObject (readonly)

Returns the value of attribute value.



3
4
5
# File 'lib/properb/shrink_tree.rb', line 3

def value
  @value
end

Instance Method Details

#debugObject



40
41
42
# File 'lib/properb/shrink_tree.rb', line 40

def debug
  { @value => @children.map(&:debug).to_a }
end

#flat_map(&block) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/properb/shrink_tree.rb', line 17

def flat_map(&block)
  result = block.call(@value)
  # First try to shrink the parent generator, then try to shrink
  # the child.
  combined_children = (children.map { _1.flat_map(&block) } + result.children)
  ShrinkTree.new(result.value, combined_children)
end

#map(&block) ⇒ Object



10
11
12
13
14
15
# File 'lib/properb/shrink_tree.rb', line 10

def map(&block)
  ShrinkTree.new(
    block.call(@value),
    @children.map { _1.map(&block) }
  )
end

#select(&block) ⇒ Object

Return an array of nodes to use to replace the current node, filtered by calling the provided block on the node’s values.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/properb/shrink_tree.rb', line 27

def select(&block)
  if block.call(@value)
    [
      ShrinkTree.new(
        @value,
        @children.flat_map { _1.select(&block) }
      )
    ]
  else
    @children.flat_map { _1.select(&block) }
  end
end