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.7 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_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 A18EB1F463 for ; Thu, 26 Dec 2019 23:13:46 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 2CB6E120A6B; Fri, 27 Dec 2019 08:13:31 +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 DDF17120A69 for ; Fri, 27 Dec 2019 08:13:28 +0900 (JST) Received: by filterdrecv-p3las1-5bf99c48d-2gb8k with SMTP id filterdrecv-p3las1-5bf99c48d-2gb8k-18-5E053E9A-14 2019-12-26 23:13:30.249392038 +0000 UTC m=+858464.257801713 Received: from herokuapp.com (unknown [54.84.189.171]) by ismtpd0117p1mdw1.sendgrid.net (SG) with ESMTP id yKpSPti0R966_yaE-HY-pA for ; Thu, 26 Dec 2019 23:13:30.088 +0000 (UTC) Date: Thu, 26 Dec 2019 23:13:30 +0000 (UTC) From: sawadatsuyoshi@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 72165 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16441 X-Redmine-Issue-Author: zverok 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: =?us-ascii?Q?jFXA8Rt481sXUUIO9tYW1AJlMOZdNdlSw=2F5TfLCefGvuKKVT+K5Ed+7MSo6Int?= =?us-ascii?Q?q+tmHhLW4OJKI9nLjQSkwDdDM1Y7HuHd9VKVd3C?= =?us-ascii?Q?1gOArTu0HQFduJdQujzpQJhDWil3sR=2FJ+XlYLvJ?= =?us-ascii?Q?OU1zbEBo=2Ftb00f97KsMV9TrSV4sY1jkYFpcMZCv?= =?us-ascii?Q?HTKb5LIeltCbH7M+0gQ12v03LH35q=2Fuckb1FGOx?= =?us-ascii?Q?t6M=2FdPAhmAWc5xYYI=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 96506 Subject: [ruby-core:96506] [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 sawa (Tsuyoshi Sawada). Dan0042 (Daniel DeLorme) wrote: > `take_while_after` is rather unwieldy, so in terms of naming maybe I can suggest: > > ```ruby > str.each_line(chomp: true).take_from{ _1 == '<<' }.take_upto{ _1 == '>>' } > ``` Such methods are the same as the cases 6 an 7 in my proposal #16446. ---------------------------------------- Feature #16441: Enumerable#take_while_after https://bugs.ruby-lang.org/issues/16441#change-83438 * Author: zverok (Victor Shepelev) * Status: Open * 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/