From: sawadatsuyoshi@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:96399] [Ruby master Feature#16441] Enumerable#take_while_after
Date: Sun, 22 Dec 2019 05:21:27 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-83324.20191222052126.4470843739d791a2@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16441.20191221132215@ruby-lang.org
Issue #16441 has been updated by sawa (Tsuyoshi Sawada).
Not necessarily against the proposal, but just to note. For the second example, I would do:
```ruby
'7+38/6'.split(/(\D+)/) # => ["7", "+", "38", "/", "6"]
```
----------------------------------------
Feature #16441: Enumerable#take_while_after
https://bugs.ruby-lang.org/issues/16441#change-83324
* 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 = <<DOC
prologue
<<
1
2
3
>>
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/
next prev parent reply other threads:[~2019-12-22 5:21 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-16441.20191221132215@ruby-lang.org>
2019-12-21 13:22 ` [ruby-core:96385] [Ruby master Feature#16441] Enumerable#take_while_after zverok.offline
2019-12-21 14:01 ` [ruby-core:96387] " nobu
2019-12-21 16:12 ` [ruby-core:96391] " shevegen
2019-12-22 5:21 ` sawadatsuyoshi [this message]
2019-12-22 5:46 ` [ruby-core:96401] " sawadatsuyoshi
2019-12-26 22:32 ` [ruby-core:96505] " daniel
2019-12-26 23:13 ` [ruby-core:96506] " sawadatsuyoshi
2020-01-14 3:24 ` [ruby-core:96831] " mame
2020-01-14 5:05 ` [ruby-core:96834] " akr
2020-01-16 6:53 ` [ruby-core:96892] " matz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-83324.20191222052126.4470843739d791a2@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).