Class: Properb::ShrinkTree
- Inherits:
-
Object
- Object
- Properb::ShrinkTree
- Defined in:
- lib/properb/shrink_tree.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #debug ⇒ Object
- #flat_map(&block) ⇒ Object
-
#initialize(value, children) ⇒ ShrinkTree
constructor
A new instance of ShrinkTree.
- #map(&block) ⇒ Object
-
#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.
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
#children ⇒ Object (readonly)
Returns the value of attribute children.
3 4 5 |
# File 'lib/properb/shrink_tree.rb', line 3 def children @children end |
#value ⇒ Object (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
#debug ⇒ Object
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 |