Actions
Feature #13765
openAdd Proc#bind
Feature #13765:
Add Proc#bind
Status:
Open
Assignee:
-
Target version:
-
Description
Proc
has curry
but no method to do partial application. Something like Proc#bind
might be handy.
A naive implementation might look something like
class Proc
def bind(*bound_args)
-> (*args) { self.call(*bound_args, *args) }
end
end
irb(main):001:0> foo = -> (first, second) { puts first, second }
=> #<Proc:0x007fc93a091f90@(irb):6 (lambda)>
irb(main):002:0> foo.bind(1).call(2)
1
2
=> nil
irb(main):003:0> foo.bind(1).bind(2).call
1
2
which does the job with the downside of only reporting argument mismatches when the returned Proc
is called.
irb(main):004:0> foo3 = foo.bind(1).bind(2).bind(3)
=> #<Proc:0x007fc9378bcb00@(irb):3 (lambda)>
irb(main):005:0> foo.call
ArgumentError: wrong number of arguments (given 0, expected 2)
from (irb):6:in `block in irb_binding'
from (irb):35
from /usr/local/bin/irb:11:in `<main>'
Actions