Sha256: 5887e3bc0026da5528f4d5ce1f59ccc90c503bf2b31629a2b8d7ef29b2cc7146

Contents?: true

Size: 776 Bytes

Versions: 13

Compression:

Stored size: 776 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 prok.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

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

    instance_eval rec
  end

end

Version data entries

13 entries across 12 versions & 1 rubygems

Version Path
facets-2.9.3 lib/core/facets/proc/curry.rb
facets-2.9.2 src/core/facets/proc/curry.rb
facets-2.9.2 lib/core/facets/proc/curry.rb
facets-2.9.1 lib/core/facets/proc/curry.rb
facets-2.9.0 lib/core/facets/proc/curry.rb
facets-2.9.0.pre.2 lib/core/facets/proc/curry.rb
facets-2.9.0.pre.1 lib/core/facets/proc/curry.rb
facets-2.8.4 lib/core/facets/proc/curry.rb
facets-2.8.3 lib/core/facets/proc/curry.rb
facets-2.8.2 lib/core/facets/proc/curry.rb
facets-2.8.1 lib/core/facets/proc/curry.rb
facets-2.8.0 lib/core/facets/proc/curry.rb
facets-2.7.0 lib/core/facets/proc/curry.rb