Sha256: 4ee7e9534e55df5bf869c5940ca0f334b43b12ceb12120ee390954537d80a3bc

Contents?: true

Size: 903 Bytes

Versions: 5

Compression:

Stored size: 903 Bytes

Contents

module Option

  def option(value)
    value.nil? ? none : some(value)
  end

  def some(value)
    Some.new(value)
  end

  def none
    NONE
  end

  class Option
    def is_defined?
      !is_empty?
    end
  end

  class Some < Option
    include Comparable
    attr_reader :value

    def initialize(value)
      @value = value
    end

    def map(fn)
      some(fn.(value))
    end

    def enumerator
      Enumerator.new { |y|
        y << @value
        raise StopIteration.new
      }
    end

    def is_empty?
      false
    end

    def <=>(other)
      @value <=> other.value
    end

    def to_s
      "Some(#{value})"
    end
  end

  class None < Option
    def is_empty?
      true
    end

    def map(fn)
      none
    end

    def enumerator
      Enumerator.new { |y|
        raise StopIteration.new
      }
    end

    def to_s
      'None'
    end
  end

  NONE=None.new
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
totally_lazy-0.1.12 lib/option.rb
totally_lazy-0.1.11 lib/option.rb
totally_lazy-0.1.10 lib/option.rb
totally_lazy-0.1.9 lib/option.rb
totally_lazy-0.1.0 lib/option.rb