Sha256: 8c95e3dd3653efca64a8dcdb91f59bed20f4c6e8b440b97c00bf65153a682b51

Contents?: true

Size: 961 Bytes

Versions: 4

Compression:

Stored size: 961 Bytes

Contents

# frozen_string_literal: true

module Fear
  # @api private
  class PartialFunctionClass
    include PartialFunction

    # @param condition [#call] describes the domain of partial function
    # @param function [Proc] function definition
    def initialize(condition, &function)
      @condition = condition
      @function = function
    end
    attr_reader :condition, :function
    private :condition
    private :function

    # @param arg [any]
    # @return [any] Calls this partial function with the given argument when it
    #   is contained in the function domain.
    # @raise [MatchError] when this partial function is not defined.
    def call(arg)
      call_or_else(arg, &PartialFunction::EMPTY)
    end

    # @param arg [any]
    # @yield [arg] if function not defined
    def call_or_else(arg)
      if defined_at?(arg)
        function.(arg)
      else
        yield arg
      end
    end
  end

  private_constant :PartialFunctionClass
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fear-2.0.1 lib/fear/partial_function_class.rb
fear-2.0.0 lib/fear/partial_function_class.rb
fear-1.2.0 lib/fear/partial_function_class.rb
fear-1.1.0 lib/fear/partial_function_class.rb