Class: SoberSwag::Nodes::Binary

Inherits:
Base
  • Object
show all
Defined in:
lib/sober_swag/nodes/binary.rb

Overview

A binary node: has a left and right hand side. Basically a node of a binary tree.

Direct Known Subclasses

Sum

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#<=>, #eql?, #hash

Constructor Details

#initialize(lhs, rhs) ⇒ Binary

Returns a new instance of Binary.

Parameters:



10
11
12
13
# File 'lib/sober_swag/nodes/binary.rb', line 10

def initialize(lhs, rhs)
  @lhs = lhs
  @rhs = rhs
end

Instance Attribute Details

#lhsSoberSwag::Nodes::Base (readonly)

Returns the left-hand node.

Returns:



17
18
19
# File 'lib/sober_swag/nodes/binary.rb', line 17

def lhs
  @lhs
end

#rhsSoberSwag::Nodes::Base (readonly)

Returns the right-hand node.

Returns:



21
22
23
# File 'lib/sober_swag/nodes/binary.rb', line 21

def rhs
  @rhs
end

Instance Method Details

#cata(&block) ⇒ Object

Maps over the LHS side first, then the RHS side, then the root.



41
42
43
44
45
46
47
48
# File 'lib/sober_swag/nodes/binary.rb', line 41

def cata(&block)
  block.call(
    self.class.new(
      lhs.cata(&block),
      rhs.cata(&block)
    )
  )
end

#deconstructArray(SoberSwag::Nodes::Base, SoberSwag::Nodes::Base)

Deconstructs into an array of [lhs, rhs]



27
28
29
# File 'lib/sober_swag/nodes/binary.rb', line 27

def deconstruct
  [lhs, rhs]
end

#deconstruct_keys(_keys) ⇒ Object

Deconstruct into a hash of attributes.



33
34
35
# File 'lib/sober_swag/nodes/binary.rb', line 33

def deconstruct_keys(_keys)
  { lhs: lhs, rhs: rhs }
end

#map(&block) ⇒ Object

Maps over the LHS side first, then the RHS side, then the root.



54
55
56
57
58
59
# File 'lib/sober_swag/nodes/binary.rb', line 54

def map(&block)
  self.class.new(
    lhs.map(&block),
    rhs.map(&block)
  )
end