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.6 required=3.0 tests=AWL,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_PASS shortcircuit=no autolearn=ham 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 9E32F1F803 for ; Wed, 9 Jan 2019 10:27:19 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 91CF7121C75; Wed, 9 Jan 2019 19:27: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 C334F121C3C for ; Wed, 9 Jan 2019 19:27:14 +0900 (JST) Received: by filter0078p3mdw1.sendgrid.net with SMTP id filter0078p3mdw1-5375-5C35CC7F-4C 2019-01-09 10:27:11.675553793 +0000 UTC m=+123562.937662279 Received: from herokuapp.com (ec2-54-82-77-37.compute-1.amazonaws.com [54.82.77.37]) by ismtpd0004p1iad1.sendgrid.net (SG) with ESMTP id j-EGMmfkQIOFNz_dYnAHzg Wed, 09 Jan 2019 10:27:11.615 +0000 (UTC) Date: Wed, 09 Jan 2019 10:27:12 +0000 (UTC) From: nobu@ruby-lang.org To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 66375 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15483 X-Redmine-Issue-Author: aycabta X-Redmine-Sender: nobu 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: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS6hj9ujiWdFHVzt7f1VDMDOeeIoOPOdkr84uB N6JM0t8g9t/q4OtX2GLvlwa10Rvm/A3wUYAjUv7tRk+nEa3cI6hX0XbKq3J/MJ1MryOWGrU4lgU8+w oCTO89bFS+rsiY5+zctRwcqfEQouTxMGK+Cb42Pjw7gb2GbIF44EqDXzgw== X-ML-Name: ruby-core X-Mail-Count: 90944 Subject: [ruby-core:90944] [Ruby trunk Feature#15483] Proc or Method combination with Symbol 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 #15483 has been updated by nobu (Nobuyoshi Nakada). I made [function-composite](https://rubygems.org/gems/function-composite) gem. ---------------------------------------- Feature #15483: Proc or Method combination with Symbol https://bugs.ruby-lang.org/issues/15483#change-76146 * Author: aycabta (aycabta .) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- In [Feature #6284], Matz said > We need more discussion if we would add combination methods to the Symbol class. Right, let's get started to discuss. For your information, recent a few months I'm discussing this with @osyo . ## This is a discussion of "design" I understand that all features of this issue have both merits and demerits, but I guess that language design is most important. All features of this issue related to each other. ## Abstract At present, you can use `Proc#>>` or `Proc#<<` with `Symbol#to_proc`. ```ruby %w{72 101 108 108 111}.map(&(:to_i.to_proc >> :chr.to_proc)) # => ["H", "e", "l", "l", "o"] ``` This is convenient but methods that take block can take a proc with `&` syntax sugar instead of `#to_proc` by right, like `[1, 2, 3].map(&:to_s)`. So `Symbol#to_proc` looks like too long for `Proc#>>` or `Proc#<<`. Therefore, you need new syntax sugar. ## Receiver ### `Symbol#>>` and `Symbol#<<` `Symbol#>>` and `Symbol#<<` will be considered, but this means that `Symbol` is treated as `Proc` partially. The `[1, 2, 3].map(&:to_s)` treats `Symbol` as `Proc` partially too, but it's with pre-positioned `&`. ```ruby %w{72 101 108 108 111}.map(&(:to_i >> :chr.to_proc)) # => ["H", "e", "l", "l", "o"] ``` I can't come up with other ideas for the `Symbol` receiver. ### New `&:symbol_name` syntax sugar for `:symbol_name.to_proc` ```ruby %w{72 101 108 108 111}.map(&(&:to_i >> :chr.to_proc))) # => ["H", "e", "l", "l", "o"] ``` ## Argument ### Calls `#to_proc` by `Proc#>>` or `Proc#<<` internally as a duck typing ```ruby %w{72 101 108 108 111}.map(&(:to_i.to_proc >> :chr)) # => ["H", "e", "l", "l", "o"] ``` In this case, `Proc#>>`(`:to_i.to_proc >>`) calls `Symbol#to_proc`(for `:chr`) inside. This is useful to use with `Hash#to_proc`: ```ruby h = { Alice: 30, Bob: 60, Cris: 90 } %w{Alice Bob Cris}.map(&(:to_sym.to_proc >> h)) # => [30, 60, 90] ``` ### `Proc#>>` and `Proc#<<` take block as an argument ```ruby %w{72 101 108 108 111}.map(&(:to_i.to_proc >> &:chr)) ``` ## Combination of receiver and argument `Symbol#>>` and calling `#to_proc` internally: ```ruby %w{72 101 108 108 111}.map(&(:to_i >> :chr)) # => ["H", "e", "l", "l", "o"] ``` `&:symbol_name` syntax sugar for `:symbol_name.to_proc` and `Symbol#>>` and taking block: ```ruby %w{72 101 108 108 111}.map(&(&:to_i >> &:chr)) # => ["H", "e", "l", "l", "o"] ``` -- https://bugs.ruby-lang.org/