Sha256: 82df3670b73542aa1de0fd66d9ec9c431785775aacbe75f9cdd89820c7dd763c

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

require 'ludy/version'

if Ludy.ruby_before '1.9.0'
  require 'ludy/kernel/public_send'
  class Proc
    def __curry__ *pre # :nodoc:
      lambda{ |*post| self[*(pre + post)] }
    end

    # make the caller be a curried function
    #   lambda{|a,b,c| [a,b,c]}.curry[1][2][3]
    #   => [1,2,3]
    def curry
      class << self
        alias_method :__call__, :call
        def call *args, &block # :nodoc:
          if self.arity == -1
            begin # let's try if arguments are ready
              # is there any better way to determine this?
              # it's hard to detect correct arity value when
              # Symbol#to_proc happened
              # e.g., :message_that_you_never_know.to_proc.arity => ?
              # i'd tried put hacks in Symbol#to_proc, but it's
              # difficult to implement in correct way
              # i would try it again in other day
              self.public_send :__call__, *args, &block
            rescue ArgumentError # oops, let's curry it
              method(:call).to_proc.public_send :__curry__, *args
            end
          elsif args.size == self.arity
            self.public_send :__call__, *args, &block
          else
            method(:call).to_proc.public_send :__curry__, *args
          end
        end
        alias_method :[], :call
      end
      self
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ludy-0.1.15 lib/ludy/proc/curry.rb