From: ritchie@...
Date: 2018-11-14T06:56:36+00:00
Subject: [ruby-core:89791] [Ruby trunk Feature#15302] Proc#with and Proc#by, for partial function application and currying

Issue #15302 has been reported by RichOrElse (Ritchie Buitre).

----------------------------------------
Feature #15302: Proc#with and Proc#by, for partial function application and currying
https://bugs.ruby-lang.org/issues/15302

* Author: RichOrElse (Ritchie Buitre)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
**Proc#by** allows currying  implicitly
~~~ ruby
class Proc
  def by(*head)
    return self if head.none?
    curry(head.size.next).(*head)
  end
end

class Method
  def by(*head)
    to_proc.by(*head)
  end
end

class Symbol
  def by(*head)
    to_proc.by(*head)
  end
end

double = :*.by(2) # => proc { |n| 2 * n }
~~~

**Proc#with** pre-defines trailing arguments and/or block. 
~~~ ruby
class Proc
  def with(*tail, &blk)
    if arity == tail.size.next
      proc { |head| call head, *tail, &blk }
    else
      proc { |*head| call *head, *tail, &blk }
    end
  end
end

class Method
  def with(*head, &blk)
    to_proc.with(*head, &blk)
  end
end

class Symbol
  def with(*head, &blk)
    to_proc.with(*head, &blk)
  end
end

double = :*.with(2) # => proc { |n| n * 2 }
~~~

That's the basic idea, but I've also expanded on it by optimising and defining operators (+, &, |) and other methods (Proc#such) [here](https://gist.github.com/RichOrElse/12d056be5757ec7ce540708bbac2b584).




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>