From: "zverok (Victor Shepelev) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "zverok (Victor Shepelev)" <noreply@ruby-lang.org>
Subject: [ruby-core:118974] [Ruby master Feature#20664] Add `before` and `until` options to Enumerator.produce
Date: Wed, 28 Aug 2024 11:36:56 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-109547.20240828113656.8@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-20664.20240803164157.8@ruby-lang.org>
Issue #20664 has been updated by zverok (Victor Shepelev).
About does/doesn’t include the last element, there is a related inconclusive discussion: #18136
Basically, I believe that both `take_while`/`take_until` might have pairs of use cases:
```ruby
# the last element not necessary
sequence.take_until(&:bad?)
# the last element is necessary: think `.` in a sentence
sequence.take_until(&:terminator?)
# the last element is not necessary
sequence.take_while(&:suitable?)
# the last element is necessary: think the last page on pagination
# (which doesn’t match the condition “have more pages” but is a meaningful element itself)
sequence.take_while(&:last_matching_element?)
```
(We have a `#take_while_after` in our `core_ext.rb`, and use it regularly.)
The `Enumerable#slice_after` and `#slice_before`, for example, recognize that it might be either, but not `take_while`.
----------------------------------------
Feature #20664: Add `before` and `until` options to Enumerator.produce
https://bugs.ruby-lang.org/issues/20664#change-109547
* Author: knu (Akinori MUSHA)
* Status: Open
----------------------------------------
Enumerator.produce provides a nice way to generate an infinite sequence but is a bit awkward to define how to end a sequence. It lacks a simple and easy way to produce typical finite sequences in an intuitive syntax.
This proposal attempts to solve the problem by adding these two options to the method:
- `before`: when provided, it is used as a predicate to determine if an iteration should end before a generated value gets yielded.
- `until`: when provided, it is used as a predicate to determine if an iteration should end until after a generated value gets yielded.
Any value that responds to `to_proc` and returns a `Proc` object is accepted in these options.
A typical use case for the `before` option is traversing a tree structure to iterate over the ancestors or following/preceding siblings of a node.
The `until` option can be used when there is a clear definition of the "last" value to yield.
```ruby
enum = Enumerator.produce(File, before: :nil?, &:superclass)
enum.to_a #=> [File, IO, Object, BasicObject]
enum = Enumerator.produce(3, until: :zero?, &:pred)
enum_to_a #=> [3, 2, 1, 0]
```
---Files--------------------------------
0001-Add-before-and-until-options-to-Enumerator.produce.patch (10.7 KB)
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
next prev parent reply other threads:[~2024-08-28 11:37 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-03 16:41 [ruby-core:118784] " knu (Akinori MUSHA) via ruby-core
2024-08-03 17:31 ` [ruby-core:118785] " zverok (Victor Shepelev) via ruby-core
2024-08-03 18:22 ` [ruby-core:118786] " knu (Akinori MUSHA) via ruby-core
2024-08-03 18:39 ` [ruby-core:118787] " knu (Akinori MUSHA) via ruby-core
2024-08-19 21:05 ` [ruby-core:118896] " matheusrich (Matheus Richard) via ruby-core
2024-08-19 23:51 ` [ruby-core:118902] " ufuk (Ufuk Kayserilioglu) via ruby-core
2024-08-25 18:24 ` [ruby-core:118955] " zverok (Victor Shepelev) via ruby-core
2024-08-26 12:14 ` [ruby-core:118959] " Eregon (Benoit Daloze) via ruby-core
2024-08-26 12:38 ` [ruby-core:118960] " matheusrich (Matheus Richard) via ruby-core
2024-08-26 16:13 ` [ruby-core:118962] " Eregon (Benoit Daloze) via ruby-core
2024-08-28 11:36 ` zverok (Victor Shepelev) via ruby-core [this message]
2024-08-28 14:07 ` [ruby-core:118978] " matheusrich (Matheus Richard) via ruby-core
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-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-109547.20240828113656.8@ruby-lang.org \
--to=ruby-core@ml.ruby-lang.org \
--cc=noreply@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 NNTP newsgroup(s).