Project

General

Profile

Feature #13765

Add Proc#bind

Added by davidcornu (David Cornu) about 3 years ago. Updated about 3 years ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:82170]

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>'

Related issues

Related to Ruby master - Feature #6817: Partial applicationOpenmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Feature #7939: Alternative curry function creationFeedbackmatz (Yukihiro Matsumoto)Actions

Also available in: Atom PDF