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.9 required=3.0 tests=AWL,BAYES_00, 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 F3E751F466 for ; Thu, 16 Jan 2020 06:53:19 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 90C03120ABE; Thu, 16 Jan 2020 15:53:05 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 5FF6F1208BD for ; Thu, 16 Jan 2020 15:53:03 +0900 (JST) Received: by filterdrecv-p3mdw1-56c97568b5-xjbx9 with SMTP id filterdrecv-p3mdw1-56c97568b5-xjbx9-19-5E200857-10 2020-01-16 06:53:11.481139295 +0000 UTC m=+2614204.531041176 Received: from herokuapp.com (unknown [54.158.130.18]) by ismtpd0004p1iad1.sendgrid.net (SG) with ESMTP id -uUvZQzRSKKpfTR6N7samw for ; Thu, 16 Jan 2020 06:53:11.396 +0000 (UTC) Date: Thu, 16 Jan 2020 06:53:11 +0000 (UTC) From: matz@ruby.or.jp Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 72554 X-Redmine-Project: ruby-master X-Redmine-Issue-Id: 16441 X-Redmine-Issue-Author: zverok X-Redmine-Sender: matz 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?bXEIHGfdFwsIlBTndiToCp=2Fmc2rfxRD2sZAksRKJIHUtCQPNGrcUUSrnxDr9Xk?= =?us-ascii?Q?pZanrm+MhAN6MrYVlniLY9=2FkGc7AQYSfFjEcwOm?= =?us-ascii?Q?pZMkzHRK8HcAS19ZGxCYg+t5M4cFG10s27ifcyB?= =?us-ascii?Q?bst8tJLoxIHIc8DQQ3jhlkiiuUsdU9WonE2Zpts?= =?us-ascii?Q?wRbzYY5DuspC0QFh2yu6xbsGUpjkUpQDQYo75rV?= =?us-ascii?Q?q=2F=2FQTZUQJlJVzb6zQ=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 96892 Subject: [ruby-core:96892] [Ruby master Feature#16441] Enumerable#take_while_after 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 #16441 has been updated by matz (Yukihiro Matsumoto). Status changed from Open to Rejected I don't see the real-world usage of `take_while_after`. Use `take_while` with proper condition. Matz. ---------------------------------------- Feature #16441: Enumerable#take_while_after https://bugs.ruby-lang.org/issues/16441#change-83906 * Author: zverok (Victor Shepelev) * Status: Rejected * Priority: Normal * Assignee: * Target version: ---------------------------------------- The method is just like `#take_while`, but also includes the item where condition became true. Examples of usefulness: ```ruby str = <> epilogue DOC ``` Imagine we want to take everything starting from `<<` to `>>` in short and clean Ruby. Surprisingly, our best guess would be infamous flip-flop: ```ruby str.each_line(chomp: true).filter_map { _1 if _1 == '<<'.._1 == '>>' } # => ["<<", "1", "2", "3", ">>"] ``` Trying to achieve this with `Enumerator`, you _almost_ can express it, but the last line is lost: ```ruby str.each_line(chomp: true).drop_while { _1 != '<<' }.take_while { _1 != '>>' } # => ["<<", "1", "2", "3"] ``` So, Enumerable leaves us with this (which is harder to read, due to additional `.first`): ```ruby str.each_line(chomp: true).drop_while { _1 != '<<' }.slice_after { _1 == '>>' }.first # => ["<<", "1", "2", "3", ">>"] ``` With proposed method: ```ruby str.each_line(chomp: true).drop_while { _1 != '<<' }.take_while_after { _1 != '>>' } # => ["<<", "1", "2", "3", ">>"] ``` The idea is the same as with flip-flops `..` vs `...` (sometimes we need to include the last element matching the condition, sometimes don't), and `while ... end` vs `do ... while`. Another example (from `Enumerator.produce` [proposal](https://bugs.ruby-lang.org/issues/14781)): ```ruby require 'strscan' scanner = StringScanner.new('7+38/6') Enumerator.produce { scanner.scan(%r{\d+|[-+*/]}) }.take_while { !scanner.eos? } # => ["7", "+", "38", "/"] Enumerator.generate { scanner.scan(%r{\d+|[-+*/]}) }.slice_after { scanner.eos? }.first # => ["7", "+", "38", "/", "6"] Enumerator.produce { scanner.scan(%r{\d+|[-+*/]}) }.take_while_after { !scanner.eos? } # => ["7", "+", "38", "/", "6"] ``` PS: Not sure about the name, suggestions are welcome -- https://bugs.ruby-lang.org/