Sha256: cecb84ed9ccd2b76efe134493b771e39d5ad829f5d7c6d6e6e06ade29b2a7f50

Contents?: true

Size: 1.07 KB

Versions: 11

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module Kind
  class Either::Monad
    require 'kind/either/monad/wrapper'

    attr_reader :value

    singleton_class.send(:alias_method, :[], :new)

    def initialize(value)
      @value = value
    end

    def left?
      false
    end

    def right?
      false
    end

    def value_or(_method_name = UNDEFINED, &block)
      raise NotImplementedError
    end

    def map(&_)
      raise NotImplementedError
    end

    alias_method :map!, :map
    alias_method :then, :map
    alias_method :then!, :map
    alias_method :and_then, :map
    alias_method :and_then!, :map

    def on
      monad = Wrapper.new(self)

      yield(monad)

      monad.output
    end

    def on_right(matcher = UNDEFINED)
      yield(value) if right? && either?(matcher)

      self
    end

    def on_left(matcher = UNDEFINED)
      yield(value) if left? && either?(matcher)

      self
    end

    def either?(matcher)
      UNDEFINED == matcher || matcher === value
    end

    def ===(monad)
      self.class === monad && self.value === monad.value
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
kind-5.10.0 lib/kind/either/monad.rb
kind-5.9.0 lib/kind/either/monad.rb
kind-5.8.1 lib/kind/either/monad.rb
kind-5.8.0 lib/kind/either/monad.rb
kind-5.7.0 lib/kind/either/monad.rb
kind-5.6.0 lib/kind/either/monad.rb
kind-5.5.0 lib/kind/either/monad.rb
kind-5.4.1 lib/kind/either/monad.rb
kind-5.4.0 lib/kind/either/monad.rb
kind-5.3.0 lib/kind/either/monad.rb
kind-5.2.0 lib/kind/either/monad.rb