Project

General

Profile

Actions

Feature #13765

open

Add Proc#bind

Added by davidcornu (David Cornu) over 7 years ago. Updated over 7 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:82154]

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 2 (1 open1 closed)

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

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0