From: me@... Date: 2017-07-26T13:31:23+00:00 Subject: [ruby-core:82177] [Ruby trunk Feature#13765] Add Proc#bind Issue #13765 has been updated by davidcornu (David Cornu). > Could you show a real Ruby application or code which you can write more effectively if we have partial application? The use case is similar to that of `Proc#curry`, but I'd agree that typical Ruby code doesn't rely on `Proc`s much. The lack of partial application on `Proc` just seemed like an odd omission. The particular code I was writing that led to this implemented pagination by returning the current page of results and a proc to fetch the next page. Example: ~~~ ruby bind = -> (fn, *bound_args) { -> (*args) { fn.(*bound_args, *args) } } fetch_page = -> (page = 1) { # Perform request [results, bind.(fetch_page, page + 1)] } ~~~ which lets you use it as follows ~~~ ruby results, next_page = fetch_page.() until results.empty? # Process results results, next_page = next_page.() end ~~~ ---------------------------------------- Feature #13765: Add Proc#bind https://bugs.ruby-lang.org/issues/13765#change-65936 * Author: davidcornu (David Cornu) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- `Proc` has [`curry`](https://ruby-doc.org/core-2.4.1/Proc.html#method-i-curry) but no method to do [partial application](https://en.wikipedia.org/wiki/Partial_application). Something like `Proc#bind` might be handy. A naive implementation might look something like ~~~ ruby class Proc def bind(*bound_args) -> (*args) { self.call(*bound_args, *args) } end end ~~~ ~~~ text irb(main):001:0> foo = -> (first, second) { puts first, second } => # 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) => # 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 `
' ~~~ -- https://bugs.ruby-lang.org/ Unsubscribe: