par / lib / par / result.rb

module Par
  class Result
    attr_reader :obj

    def initialize obj
      @obj = obj
    end

    def >> other
      # Concatenation with another Result
      if other.respond_to? :obj
        return concat(obj, other.obj)
      end

      # Concatenation with bottom
      return bottom if other == bottom
      return self if other == top

      # Wild concatenation with any other object
      return concat(obj, other)
    end
    def maybe
      self
    end
    def as replacement=nil, &block
      unless block_given?
        return Result.new(replacement)
      end

      return Result.new(block.call(@obj))
    end

    def new obj
      self.class.new obj
    end
    def bottom
      self.class.bottom
    end
    def top
      self.class.top
    end

    def bottom?
      false
    end
    
    def concat obj1, obj2
      new(obj1 + obj2)
    end

    def to_str
      obj.to_str
    end
    def to_s
      obj.to_s
    end
    def inspect
      "result(#{obj.inspect})"
    end

    def <=> other
      obj <=> other
    end
    include Comparable

    def self.bottom
      @bottom ||= Bottom.new
    end
    def self.top
      @top ||= Top.new
    end
  end
end

require 'par/result/top'
require 'par/result/bottom'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.