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=-4.1 required=3.0 tests=BAYES_00,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 BD9761F463 for ; Sat, 21 Dec 2019 14:01:25 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id EA7B3120A65; Sat, 21 Dec 2019 23:01:11 +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 F0287120A63 for ; Sat, 21 Dec 2019 23:01:07 +0900 (JST) Received: by filterdrecv-p3las1-5bf99c48d-j57ff with SMTP id filterdrecv-p3las1-5bf99c48d-j57ff-19-5DFE25A9-35 2019-12-21 14:01:13.323558231 +0000 UTC m=+393328.962912476 Received: from herokuapp.com (unknown [75.101.247.47]) by ismtpd0030p1iad2.sendgrid.net (SG) with ESMTP id tNiawWp5S0qs9KWXlztyrw for ; Sat, 21 Dec 2019 14:01:13.118 +0000 (UTC) Date: Sat, 21 Dec 2019 14:01:13 +0000 (UTC) From: nobu@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 72047 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16441 X-Redmine-Issue-Author: zverok 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: =?us-ascii?Q?q8Dly+pU2+3ektTtZVXgZtbJPXwqo7p86jCsvYTW4BwMCIqf0quijYbSysi0ay?= =?us-ascii?Q?M5eZYld8qAms3+o7IAyMnXAx46WR5PqgRplNmdL?= =?us-ascii?Q?+o+9mrJeluH01J=2FsDfovOq4TX5iyUl3BSviD5VY?= =?us-ascii?Q?Ww2p8adKTgeHqtMnM7VJwd0iQX8+lWzOOnNTNu1?= =?us-ascii?Q?C4LOx636bS0hrDrl3LqDI1trNNsrDAQ0GE+XRm1?= =?us-ascii?Q?BVK8Y0LQemhKM47xY=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 96387 Subject: [ruby-core:96387] [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 nobu (Nobuyoshi Nakada). Flip-flop is the winner!!! ---------------------------------------- Feature #16441: Enumerable#take_while_after https://bugs.ruby-lang.org/issues/16441#change-83309 * 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: p 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... p str.each_line(chomp: true).drop_while { _1 != '<<' }.take_while { _1 != '>>' } # => ["<<", "1", "2", "3"] -- the last line is lost. # So, Enumerable leaves us with this (which is harder to read, due to additional `.first`): p str.each_line(chomp: true).drop_while { _1 != '<<' }.slice_after { _1 == '>>' }.first # => ["<<", "1", "2", "3", ">>"] # With proposed method: p 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') p Enumerator.produce { scanner.scan(%r{\d+|[-+*/]}) }.take_while { !scanner.eos? } # expresses meaning, but loses last element # => ["7", "+", "38", "/"] p Enumerator.generate { scanner.scan(%r{\d+|[-+*/]}) }.slice_after { scanner.eos? }.first # slice_after {}.first again # => ["7", "+", "38", "/", "6"] p 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/