I want to create a new lambda taking no arguments from lambda taking one.
Say, I have
irb(main):001:0> f1 = lambda { |p| p }
=> #<Proc:0x00000001045190@(irb):1 (lambda)>
irb(main):002:0> f1.call(2)
=> 2
and now I
irb(main):003:0> f2 = lambda { f1.call(2) }
=> #<Proc:0x00000000f620e8@(irb):3 (lambda)>
irb(main):004:0> f2.call
=> 2
but I do not want to create a lambda around first one but want to “substitute” parameter for it or something.
May be if we have call, there is some magic that do the same thing as call, but returns a lambda except of actually calling the code:
f2 = f1.some_magic(2)
f2.call
=> 2
P.S. Sorry if this question is dumb, this functional stuff is hard to understand to me sometimes.
P.P.S. Found this topic on ruby-forum.com, and it seems I want to do the same things without unnecessary calls overhead.
You could create a function which binds the arguments to a proc. This works with Ruby >= 1.8:
In use:
If you want to bind some, but not all arguments, you can do that too:
Except that
currymakes a call rather than a new lambda if given the full arguments to the lambda being bound, this is pretty much whatcurrydoes. I didn’t call itcurryto avoid confusion with the method of that name that’s built into Ruby 1.9.