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=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,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 6BED11F462 for ; Fri, 14 Jun 2019 13:15:31 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 20A57120BE4; Fri, 14 Jun 2019 22:15:25 +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 C5C151208AC for ; Fri, 14 Jun 2019 22:15:21 +0900 (JST) Received: by filter0038p3las1.sendgrid.net with SMTP id filter0038p3las1-24124-5D039DEA-14 2019-06-14 13:15:22.267934222 +0000 UTC m=+74884.920072007 Received: from herokuapp.com (unknown [107.21.31.174]) by ismtpd0047p1mdw1.sendgrid.net (SG) with ESMTP id fwF-BXH8TNqmgY_zo8WLoQ for ; Fri, 14 Jun 2019 13:15:22.146 +0000 (UTC) Date: Fri, 14 Jun 2019 13:15:22 +0000 (UTC) From: cichol@live.cn Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 68627 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15799 X-Redmine-Issue-Author: nobu X-Redmine-Sender: cichol 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?xKcdKRGqdmOHtuK38WDbdBy=2FRdntvRMjO7+DYjcJ4=2F3CdbnUmC+Xe7pM1sbjTC?= =?us-ascii?Q?DGTGbs+AX96J76fbf3dspKgemZBV9q=2FLxKebg5K?= =?us-ascii?Q?sIYfX8DkkSBYhqNJ3L6itVs1zUjVwWMDLC12J21?= =?us-ascii?Q?Z76QKiPoGxVjeGYVbyjotG1iNPg4tne0pPEi4X5?= =?us-ascii?Q?Tvl0tXmYArchE?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 93141 Subject: [ruby-core:93141] [Ruby trunk Feature#15799] pipeline operator 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 #15799 has been updated by cichol (cichol tsai). Hi, I want to introduce a way of organizing pipelined calling in Ruby. I imagined that the Ruby-styled pipelined calling should be like: ```ruby 1.pipe do call 1 + _ # => after this line, _ becomes 2 call _ * 2 # => after this line, _ becomes 6 end # => _ is returned as 6 ``` With a seemed redundant `call` method, this `pipe` method can be implemented in current version of Ruby. I think it would be so good if we can remove the need of `call` and allow a line result capturer defined as a hook. The example now becomes: ```ruby 1.pipe do 1 + _ _ * 2 end ``` Within the block following `pipe`, results of every line are captured and passed to a hook for assignments of `_`. IMHO, this implementation of pipeline is preferable over the pipeline operator suggested above, for these reasons: 1. This method utilize the placeholder `_` to pass the argument into any position of parameters instead of the last one. If you are using pipeline operator like those in functional languages, you will need to carefully deal with the order of parameters, which adds mental overhead. And most existing Ruby methods are not implemented with currying in mind. With use of placeholder we can simply re-use previous methods without additional costs. 2. Explicitly calling by a method `pipe` and a block. This allow less aggressive modification to the language over adding operators. It is more Ruby-way and clearer. 3. The abstraction of line result capturer can be useful for other use cases. For example, if we want to inspect every steps of a pipe, we can imagine a special pipe that prints out every step: ```ruby 1.inspected_pipe do 1 + _ # => puts 2 _ * 2 # => puts 6 SomeService.new.process _ # => puts whatever the return value is end ``` It helps debugging. We can apply tiny modification to make a block loggable. 4. BTW, to avoid patheses for range, we can simply: ```ruby x = pipe do 1.. take 10 map{|e| e*2} end ``` to achieve the same functionality asked in the first post. ---------------------------------------- Feature #15799: pipeline operator https://bugs.ruby-lang.org/issues/15799#change-78570 * Author: nobu (Nobuyoshi Nakada) * Status: Closed * Priority: Normal * Assignee: * Target version: ---------------------------------------- Implemented the pipeline operator `|>`, a topic of "ruby committers vs the world" in RubyKaigi 2019. Also a casual idea of rightward assignment. ```ruby 1.. |> take 10 |> map {|e| e*2} |> (x) p x #=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] ``` https://github.com/nobu/ruby/tree/feature/pipeline -- https://bugs.ruby-lang.org/