Sha256: c4ef301e746aac95eb626cba94ccdf065770ddb80eecb0b5c4496f5c70652ac6

Contents?: true

Size: 859 Bytes

Versions: 75

Compression:

Stored size: 859 Bytes

Contents

module Tins
  module PartialApplication
    # If this module is included into a Proc (or similar object), it tampers
    # with its Proc#arity method.
    def self.included(modul)
      modul.module_eval do
        old_arity = instance_method(:arity)
        define_method(:arity) do
          defined?(@__arity__) or old_arity.bind(self).call
        end
      end
      super
    end

    # Create a partial application of this Proc (or similar object) using
    # _args_ as the already applied arguments.
    def partial(*args)
      if args.empty?
        dup
      elsif args.size > arity
        raise ArgumentError, "wrong number of arguments (#{args.size} for #{arity})"
      else
        f = lambda { |*b| call(*(args + b)) }
        f.instance_variable_set :@__arity__, arity - args.size
        f
      end
    end
  end
end

require 'tins/alias'

Version data entries

75 entries across 64 versions & 2 rubygems

Version Path
tins-1.38.0 lib/tins/partial_application.rb
tins-1.37.1 lib/tins/partial_application.rb
tins-1.37.0 lib/tins/partial_application.rb
tins-1.36.1 lib/tins/partial_application.rb
tins-1.36.0 lib/tins/partial_application.rb
tins-1.35.0 lib/tins/partial_application.rb
tins-1.34.0 lib/tins/partial_application.rb
tins-1.33.0 lib/tins/partial_application.rb
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/lib/tins/partial_application.rb
tins-1.32.1 lib/tins/partial_application.rb
tins-1.32.0 lib/tins/partial_application.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/lib/tins/partial_application.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/lib/tins/partial_application.rb
tins-1.31.1 lib/tins/partial_application.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/tins-1.31.0/lib/tins/partial_application.rb
tins-1.31.0 lib/tins/partial_application.rb
tins-1.30.0 lib/tins/partial_application.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/tins-1.29.1/lib/tins/partial_application.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/tins-1.29.1/lib/tins/partial_application.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/tins-1.29.1/lib/tins/partial_application.rb