Sha256: e99f73ec77562f05f8c57c260edfbf02163eec6e7d0c0557b7c6ad1330fdbe77

Contents?: true

Size: 1.41 KB

Versions: 9

Compression:

Stored size: 1.41 KB

Contents

module SoberSwag
  module Nodes
    ##
    # A binary node: has a left and right hand side.
    # Basically a node of a binary tree.
    class Binary < Base
      ##
      # @param lhs [SoberSwag::Nodes::Base] the left-hand node.
      # @param rhs [SoberSwag::Nodes::Base] the right-hand node.
      def initialize(lhs, rhs)
        @lhs = lhs
        @rhs = rhs
      end

      ##
      # @return [SoberSwag::Nodes::Base] the left-hand node
      attr_reader :lhs

      ##
      # @return [SoberSwag::Nodes::Base] the right-hand node
      attr_reader :rhs

      ##
      # Deconstructs into an array of `[lhs, rhs]`
      #
      # @return [Array(SoberSwag::Nodes::Base, SoberSwag::Nodes::Base)]
      def deconstruct
        [lhs, rhs]
      end

      ##
      # Deconstruct into a hash of attributes.
      def deconstruct_keys(_keys)
        { lhs: lhs, rhs: rhs }
      end

      ##
      # @see SoberSwag::Nodes::Base#cata
      #
      # Maps over the LHS side first, then the RHS side, then the root.
      def cata(&block)
        block.call(
          self.class.new(
            lhs.cata(&block),
            rhs.cata(&block)
          )
        )
      end

      ##
      # @see SoberSwag::Nodes::Base#map
      #
      # Maps over the LHS side first, then the RHS side, then the root.
      def map(&block)
        self.class.new(
          lhs.map(&block),
          rhs.map(&block)
        )
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
sober_swag-0.25.2 lib/sober_swag/nodes/binary.rb
sober_swag-0.25.1 lib/sober_swag/nodes/binary.rb
sober_swag-0.25.0 lib/sober_swag/nodes/binary.rb
sober_swag-0.24.1 lib/sober_swag/nodes/binary.rb
sober_swag-0.24.0 lib/sober_swag/nodes/binary.rb
sober_swag-0.23.0 lib/sober_swag/nodes/binary.rb
sober_swag-0.22.0 lib/sober_swag/nodes/binary.rb
sober_swag-0.21.0 lib/sober_swag/nodes/binary.rb
sober_swag-0.20.0 lib/sober_swag/nodes/binary.rb