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.2 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_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 915731F453 for ; Wed, 23 Jan 2019 09:16:03 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id EE0FB1218BC; Wed, 23 Jan 2019 18:16:00 +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 9A6211209D2 for ; Wed, 23 Jan 2019 18:15:58 +0900 (JST) Received: by filter0012p3iad2.sendgrid.net with SMTP id filter0012p3iad2-25940-5C4830CB-21 2019-01-23 09:15:55.518387856 +0000 UTC m=+734344.383699533 Received: from herokuapp.com (ec2-54-211-52-65.compute-1.amazonaws.com [54.211.52.65]) by ismtpd0050p1iad1.sendgrid.net (SG) with ESMTP id cdKYN-lFT-aU22j5p-kEAg for ; Wed, 23 Jan 2019 09:15:55.383 +0000 (UTC) Date: Wed, 23 Jan 2019 09:15:56 +0000 (UTC) From: sawadatsuyoshi@gmail.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 66668 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15557 X-Redmine-Issue-Author: sawa X-Redmine-Sender: sawa 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/Ymy4QrNMhiuLXJG8OTL2vJD1yS5gKDniy5XugS7UMH9b4tjyMCxys4oAa5xMmO wdnA4vzxZfFFQ7kgvFunfM3CwmSDDXp9pn7KFq1cYyWjY8kv/a++ZHGmTyr377juzoWDv0OGpm6nQD 7zQyhU50WWIdLDjOCxb70kcBsFWtb1uMxvc249K5/hJTgoBcA3e01Z9s+Q== X-ML-Name: ruby-core X-Mail-Count: 91220 Subject: [ruby-core:91220] [Ruby trunk Feature#15557] A new class that stores a condition and the previous receiver 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 #15557 has been reported by sawa (Tsuyoshi Sawada). ---------------------------------------- Feature #15557: A new class that stores a condition and the previous receiver https://bugs.ruby-lang.org/issues/15557 * Author: sawa (Tsuyoshi Sawada) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- I often see code like this: ```ruby foo = default_definition foo = some_method(foo) if some_condition(foo) foo = another_method(foo) if another_condition(foo) ... ``` It would be nice if we can write this as a method chain. Since we now how the method `then`, I thought it would be a nice fit to introduce a method called `when`, such that putting it right in front of `then` would execute the `then` method as ordinarily only when the condition is satisfied, and returns the previous receiver otherwise so that the code above can be rewritten as: ```ruby foo = default_definition .when{|foo| some_condition(foo)} .then{|foo| some_method(foo)} .when{|foo| another_condition(foo)} .then{|foo| another_method(foo)} ``` This proposal is also a generalization of what I intended to cover by https://bugs.ruby-lang.org/issues/13807. That is, ```ruby a.some_condition ? a : b ``` would rewritten as: ```ruby a.when(&:some_condition).then{b} ``` The proposal can be implemented by introducing a class called `Condition`, which stores a condition and the previous receiver, and works with `then` in a particular. ```ruby class Object def when Condition.new(self, yield(self)) end end class Condition def initialize default, condition @default, @condition = default, condition end def then @condition ? yield(@default) : @default end end ``` -- https://bugs.ruby-lang.org/