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=-3.0 required=3.0 tests=AWL,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 0C9411F461 for ; Mon, 19 Aug 2019 10:07:32 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id DC70B120A43; Mon, 19 Aug 2019 19:07:16 +0900 (JST) Received: from o1678916x28.outbound-mail.sendgrid.net (o1678916x28.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 2638E120A12 for ; Mon, 19 Aug 2019 19:07:13 +0900 (JST) Received: by filter0155p3mdw1.sendgrid.net with SMTP id filter0155p3mdw1-29307-5D5A74D4-64 2019-08-19 10:07:16.986695793 +0000 UTC m=+145906.067794010 Received: from herokuapp.com (unknown [54.80.45.23]) by ismtpd0011p1iad2.sendgrid.net (SG) with ESMTP id KDdwoInZSSOqRMwB9ARw3A for ; Mon, 19 Aug 2019 10:07:16.936 +0000 (UTC) Date: Mon, 19 Aug 2019 10:07:17 +0000 (UTC) From: zverok.offline@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 69980 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16113 X-Redmine-Issue-Author: zverok X-Redmine-Sender: zverok 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?3be0g8093pjUjT94eiCA64csFDBI=2FmHQTWm54P5gda6IJVSkwcijJz9cW1SXbD?= =?us-ascii?Q?lQf9bSnT3ow8X8nZif=2Fr5jWBlBKScciGZUG09Ko?= =?us-ascii?Q?SNUXobrZAnP4SKZF+0HFTNPy=2F8p84uSMhBQ6E7r?= =?us-ascii?Q?UAQU+CAHf+H5C+=2FudXgkl2Jqao2ulkhWkSO9M4z?= =?us-ascii?Q?4uCx6e6Tsbm4z2TsUmaQOw1eyu9ToDoJlaQ=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 94432 Subject: [ruby-core:94432] [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 reported by zverok (Victor Shepelev). ---------------------------------------- Feature #16113: Partial application https://bugs.ruby-lang.org/issues/16113 * 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/