Sha256: 175f2c5b2771a0bf467e005686e335191eab4f39c332afba6a0c99e3dd832d02

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

module Fear
  module PartialFunction
    # Composite function produced by +PartialFunction#and_then+ method
    # @api private
    class AndThen
      include PartialFunction

      # @param partial_function [Fear::PartialFunction]
      # @param function [Proc]
      def initialize(partial_function, &function)
        @partial_function = partial_function
        @function = function
      end
      # @!attribute partial_function
      #   @return [Fear::PartialFunction]
      # @!attribute function
      #   @return [Proc]
      attr_reader :partial_function
      attr_reader :function
      private :partial_function
      private :function

      # @param arg [any]
      # @return [any ]
      def call(arg)
        function.call(partial_function.call(arg))
      end

      # @param arg [any]
      # @return [Boolean]
      def defined_at?(arg)
        partial_function.defined_at?(arg)
      end

      # @param arg [any]
      # @yield [arg]
      # @return [any]
      def call_or_else(arg)
        result = partial_function.call_or_else(arg) do
          return yield(arg)
        end
        function.call(result)
      end
    end

    private_constant :AndThen
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fear-1.0.0 lib/fear/partial_function/and_then.rb
fear-0.11.0 lib/fear/partial_function/and_then.rb