Sha256: 5d59926e979c92a8ece3dc1cbf1e46024f1232386334f9cb9075c4a33142190c

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

module Invokable
  module Core
    # If object responds to `call` convert into a Proc forwards it's arguments along to `call`.
    #
    # @see https://ruby-doc.org/core-2.7.0/Proc.html#method-i-call Proc#call
    # @return [Proc]
    def to_proc
      if respond_to?(:call)
        # TODO: Would method(:call) be more performant? We need benchmarks.
        Proc.new do |*args|
          call(*args)
        end
      else
        raise "Don't know how to convert #{self.inspect} into a Proc"
      end
    end
  
    # Return a curried proc. If the optional `arity` argument is given, it determines the number of arguments.
    # A curried proc receives some arguments. If a sufficient number of arguments are supplied, it passes the
    # supplied arguments to the original proc and returns the result. Otherwise, returns another curried proc
    # that takes the rest of arguments.
    #
    # @see https://ruby-doc.org/core-2.7.0/Proc.html#method-i-curry Proc#curry
    # @param arity [Integer]
    # @return [Proc]
    def curry(arity = nil)
      to_proc.curry(arity)
    end
  
    # Return a memoized proc, that is, a proc that caches it's return values by it's arguments.
    #
    # @return [Proc]
    def memoize
      Proc.new do |*args|
        @memo ||= {}
        @memo[args.hash] ||= call(*args)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
invokable-0.4.2 lib/invokable/core.rb
invokable-0.4.1 lib/invokable/core.rb
invokable-0.4.0 lib/invokable/core.rb