From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-2.6 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=no autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 690591F461 for ; Fri, 23 Aug 2019 23:51:47 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id E7C9F120B34; Sat, 24 Aug 2019 08:51:39 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id ED905120A1E for ; Sat, 24 Aug 2019 08:51:37 +0900 (JST) Received: by filter0100p3iad2.sendgrid.net with SMTP id filter0100p3iad2-670-5D607C0B-13 2019-08-23 23:51:39.213781674 +0000 UTC m=+4120.494084321 Received: from herokuapp.com (unknown [3.81.9.76]) by ismtpd0028p1iad2.sendgrid.net (SG) with ESMTP id 4q0LvqZLQ2ybuNfCOHCC1Q for ; Fri, 23 Aug 2019 23:51:39.261 +0000 (UTC) Date: Fri, 23 Aug 2019 23:51:39 +0000 (UTC) From: shannonskipper@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 70066 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16113 X-Redmine-Issue-Author: zverok X-Redmine-Sender: shan X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?T0jc+HUEV7CM5SiK=2FBdwdf6zPPpzUxEkATeka3QtAYp+ynL0Z4oOUngtxnu6y3?= =?us-ascii?Q?HK8m3LB14kCNxyVKIImlm+GYlr7gb1lpnzUeO43?= =?us-ascii?Q?X8YAgtngROX1luuCeFSTaOYe6=2FnO0GoyB8tCPQl?= =?us-ascii?Q?K+lacPhr9Vtbo9Izoz+yxP7LwTe51JgL3dt2ATF?= =?us-ascii?Q?7oHxZe0J4238LIAoIb8nDGQ0TtQf67RLPqQ=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 94517 Subject: [ruby-core:94517] [Ruby master Feature#16113] Partial application X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #16113 has been updated by shan (Shannon Skipper). I like Hanmac's idea. ``` ruby Klass.:meth.curry.() ``` Seems very close to: ``` ruby Klass.:meth.w() ``` I know its previously been said that `#curry` is a bit of an easter egg. Would it be acceptable to add keyword argument support for `#curry`? @zverok If `#curry` gets support for keyword arguments, would it suffice for this case or do you think `#w` would still be called for? ---------------------------------------- Feature #16113: Partial application https://bugs.ruby-lang.org/issues/16113#change-80953 * Author: zverok (Victor Shepelev) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- **Preface:** One of the main "microstructures" of the code we use is chaining methods-with-blocks; and we really love to keep those blocks DRY when they are simple. Currently, for DRY-ing up simple blocks, we have: * `foo(&:symbol)` * `foo(&some.method(:name))` (as of 2.7, `foo(&some.:name)`) * Currently disputed "nameless block args": `foo { something(@1) }` or `foo { something(@) }` or `foo { something(it) }` **Proposal:** I argue that short and easy-to-remember partial application of blocks and methods can make methods-with-blocks much more pleasant and consistent to write, and continue softly shifting Ruby towards "functional" (while staying true to language's spirit). In order to achieve this, I propose method `{Symbol,Method,Proc}#w` (from `with`), which will produce `Proc` with _last_ arguments bound. Example of usability: ```ruby # No-shortcuts: fetch something and parse as JSON: fetch(urls).map { |body| JSON.parse(body) } # Could be already (2.7+) shortened to: fetch(urls).map(&JSON.:parse) # But if you have this: fetch(urls).map { |body| JSON.parse(body, symbolize_names: true) } # How to shorten it, to don't repeat body? # "Nameless block args" answer: fetch(urls).map { JSON.parse(@1, symbolize_names: true) } # Partial application answer: fetch(urls).map(&JSON.:parse.w(symbolize_names: true)) ``` I believe that the latter (while can be easily met with usual "hard to understand for a complete novice") provides the added value of producing proper "functional object", that can be stored in variables and constants, and generally lead to new approaches to writing Ruby code. Another example: ```ruby (6..11).map(&:**.w(2)).map(&:clamp.w(20, 50)) # => [36, 49, 50, 50, 50, 50] ``` Reference implementation: ```ruby class Symbol def w(*args) proc { |receiver, *rest| receiver.send(self, *rest, *args) } end end class Method def w(*args) proc { |receiver, *rest| self.call(receiver, *rest, *args) } end end class Proc def w(*args) prc = self proc { |*rest| prc.call(*rest, *args) } end end ``` -- https://bugs.ruby-lang.org/