From: Victor Shepelev Date: 2006-05-09T15:42:07+09:00 Subject: Re: Why not just create a new proc with the desired parameters? From: Juergen Strobel [mailto:strobel@secure.at] > >l = lambda{|a,b,c| puts "a=#{a}, b=#{b}, c=#{c}"} > > > >puts l.arity #=>3 > > > >l.bind(2, 'b') > > > >puts l.arity #=>2 > > > >l.call('a', 'c') #a=a, b=b, c=c > > >Two questions: > >1. Does somebody think it is useful? > >2. Does somebody already done this in some library? > > 1. Binding parameters is useful sometimes. > 2. Do you really need this, considering how simple it is to create a new > proc: > > adder = proc { |a,b| a+b } > add_3 = proc { |a| adder.call(a,3) } # adder bound with b=3 > Unfortunally, this wouldn't work in statements like: adder = proc { |a,b| self.output(a+b) } add_3 = proc { |a| adder.call(a,3) } # adder bound with b=3 class A def output(val) puts val end end A.new.instance_exec(4, &add_3) #want to output 4+3, but receive #undefined method `output' for main:Object (NoMethodError) Also, this bounding is less clear (somebody who would read my code, would may to think "WTF? Why this strange new proc?" So, library approach is better. > That said, there are situations a library based approach is useful. > I'd prefer solutions that create new proc objects without the bound > parameters, much like I did above man�ally: > > add_7 = bind_2nd(adder, 7) > add_7.call(1) ==> 8 > > add_11 = bind_nth(adder, 1, 11) > add_11.call(1) ==> 12 I dislike the code above. Why? Because it is not very object-oriented. I think this way: @I do the operation with Proc, so, the most natural way should be Proc#some_operation". Also, bind_1st, bind_2nd are C++-like, but I don't understand why we need such specialized methods in Ruby. At the moment, RubyMurray's approach seems to me as the most natural: adder = proc { |a,b| self.output(a+b) } add_3 = adder.curry(Curry::HOLE, 3) Here is no need to think about argument's numbers. I just provide bounding in the same order as arguments was. > > J�rgen > Victor.