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 332781F461 for ; Tue, 20 Aug 2019 01:28:30 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 9499C120A2D; Tue, 20 Aug 2019 10:28: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 4FC0A1209CF for ; Tue, 20 Aug 2019 10:28:16 +0900 (JST) Received: by filter0049p3iad2.sendgrid.net with SMTP id filter0049p3iad2-22179-5D5B4CB0-42 2019-08-20 01:28:16.909507038 +0000 UTC m=+348281.790804827 Received: from herokuapp.com (unknown [3.89.103.216]) by ismtpd0044p1mdw1.sendgrid.net (SG) with ESMTP id EIHsHQoxQsqHINaj3ZVQYg for ; Tue, 20 Aug 2019 01:28:16.781 +0000 (UTC) Date: Tue, 20 Aug 2019 01:28:16 +0000 (UTC) From: shannonskipper@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 69988 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=2FBdwdf6zPPpzUxEkATeka3QtAYq4ZLIWvtUdE7p2XrRfPx?= =?us-ascii?Q?lrixS0RUP4OANDUlkaBQhMEmvkCuD=2F4xE=2Fy5zMN?= =?us-ascii?Q?fzj1FVyQh0ALFcZ+QliNuZEUP7us0pQvjc3o0zY?= =?us-ascii?Q?CCOaibU4QhEQH=2FsDLqpA2nPk+5rIYyO9xYEuNAo?= =?us-ascii?Q?b1QAIpgyuETQmogd3CYoHhlpBC=2F=2FE3GFCwA=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 94440 Subject: [ruby-core:94440] [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). An aside, but I took a stab at a pure Ruby implementation of keyword argument currying: https://gist.github.com/havenwood/db041566abeac894602c188c77374040 ``` ruby ['{"aim":true}', '{"impossible":false}'].map &JSON.:parse.curry.(symbolize_names: true) #=> [{:aim=>true}, {:impossible=>false}] ``` ---------------------------------------- Feature #16113: Partial application https://bugs.ruby-lang.org/issues/16113#change-80866 * 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/