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=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 E9C121F461 for ; Sat, 24 Aug 2019 10:57:27 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id C92EE120935; Sat, 24 Aug 2019 19:57:18 +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 F1E4F1208D8 for ; Sat, 24 Aug 2019 19:57:15 +0900 (JST) Received: by filter0140p3las1.sendgrid.net with SMTP id filter0140p3las1-5384-5D61180C-10 2019-08-24 10:57:16.37103845 +0000 UTC m=+44705.552502788 Received: from herokuapp.com (unknown [3.81.9.76]) by ismtpd0023p1iad2.sendgrid.net (SG) with ESMTP id kXoP2I4IScqoi3nTfvszzA for ; Sat, 24 Aug 2019 10:57:16.063 +0000 (UTC) Date: Sat, 24 Aug 2019 10:57:16 +0000 (UTC) From: zverok.offline@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 70078 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=2FmHQTWm54P5gda6LthWTAIgDWsB+75Uu7V?= =?us-ascii?Q?GV7IMhajn+GZ+JfolGbe3A6mztp=2FhR4dj+7kQaA?= =?us-ascii?Q?2AAWHFOvPz9JtuYTmWwNsBYDr9I91iu0HJvjvEN?= =?us-ascii?Q?xiVgRP+mgv4LqsBcFveSYg6+NyHszPOvQd1feY8?= =?us-ascii?Q?Gq0cdgalI4c5ysdIDFAJxApu6eHXij3qVzA=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 94529 Subject: [ruby-core:94529] [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 zverok (Victor Shepelev). > If `#curry` gets support for keyword arguments, would it suffice for this case I don't think so, unfortunately. 1. Of my examples, it covers only `JSON.parse` * It doesn't cover non-keyword arguments, which, I believe, it should. Example: `construct_filename.then(&File.:read.w('rb'))` * It doesn't cover symbol partial application. While it *could* be seen as "esotheric" by some, but in realistic code, there are pretty regular demand for something like `numbers.map(&:+.w(2))`. I once [proposed](https://bugs.ruby-lang.org/issues/15301) the `numbers.map(&:+.(2))` syntax, which is "nice-looking", but semantically incorrect 2. While "conceptually true to functional programming", it is just *too long*, which kinda undermines the whole point. ---------------------------------------- Feature #16113: Partial application https://bugs.ruby-lang.org/issues/16113#change-80971 * 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/