Sha256: cfb2cf8889e1c0cc3fb686190c55c8097a3954e667bdb189ec2e8f79312a8b19

Contents?: true

Size: 759 Bytes

Versions: 11

Compression:

Stored size: 759 Bytes

Contents

class Proc

  # Curry Proc object into new Proc object.
  #
  # TODO: Utilize Ruby 1.9's #curry method.

  def curry(*args)
    if args.empty?
      idx = (0...arity).to_a
    else
      raise ArgumentError, "argument count is greater than arity (#{args.size} > #{arity})" if args.size > arity
      raise ArgumentError, "arguments must be unique indexes" if args.uniq != args
      raise ArgumentError, "arguments must be indexes" if args.any?{ |a| !Fixnum===a }
      idx = (0...arity).to_a
      idx = args + (idx - args)  
    end

    rec = ''
    idx.each do |i|
      rec << "proc { |a#{i}| "
    end
    rec << "self["
    rec << (0...arity).to_a.collect{|i| "a#{i}"}.join(',')
    rec << "]"
    rec << "}" * arity

    instance_eval rec
  end

end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
facets-2.6.0 lib/core/facets/proc/curry.rb
facets-2.4.0 lib/facets/proc/curry.rb
facets-2.4.1 lib/facets/proc/curry.rb
facets-2.4.3 lib/core/facets/proc/curry.rb
facets-2.4.2 lib/core/facets/proc/curry.rb
facets-2.4.4 lib/core/facets/proc/curry.rb
facets-2.4.5 lib/core/facets/proc/curry.rb
facets-2.5.1 lib/core/facets/proc/curry.rb
facets-2.5.0 lib/core/facets/proc/curry.rb
facets-2.5.2 lib/core/facets/proc/curry.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/proc/curry.rb