Sha256: c7eef565d32fd616bbda4955d9d4537639d946c413af681152c02afbb67f866b

Contents?: true

Size: 667 Bytes

Versions: 5

Compression:

Stored size: 667 Bytes

Contents

# -*- encoding : utf-8 -*-
module LambdaDriver::Currying

  if RUBY_VERSION < '1.9.0'
    def curry(arity = nil)
      f = self.to_proc
      arity ||= __arity(f)
      return f if arity == 0

      lambda{|arg| __curry(f, arity, arg, []) }
    end
  else
    def curry(arity = nil)
      self.to_proc.curry(arity)
    end
  end

  private
    def __arity(f)
      (f.arity >= 0) ? f.arity : -(f.arity + 1)
    end

    def __curry(f, arity, arg, passed)
      args = passed + [arg]
      return f.call(*args) if arity == 1
      lambda{|arg| __curry(f, arity - 1, arg, args) }
    end


  def self.included(klass)
    klass.send(:alias_method, :%, :curry)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lambda_driver-1.3.0 lib/lambda_driver/currying.rb
lambda_driver-1.2.4 lib/lambda_driver/currying.rb
lambda_driver-1.2.3 lib/lambda_driver/currying.rb
lambda_driver-1.2.2 lib/lambda_driver/currying.rb
lambda_driver-1.2.1 lib/lambda_driver/currying.rb