Sha256: a95c6a3ccbca8dcf96a905a3f1a6798d2d42a1db583cec3f0ddcaa60684a7043

Contents?: true

Size: 828 Bytes

Versions: 2

Compression:

Stored size: 828 Bytes

Contents

module Spruz
  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
          @__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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spruz-0.2.13 lib/spruz/partial_application.rb
spruz-0.2.12 lib/spruz/partial_application.rb