[#89806] [Ruby trunk Bug#15306] Generate prelude.c using miniruby — v.ondruch@...
Issue #15306 has been reported by vo.x (Vit Ondruch).
3 messages
2018/11/15
[ruby-core:89822] [Ruby trunk Feature#15144][Assigned] Enumerator#chain
From:
knu@...
Date:
2018-11-15 21:17:17 UTC
List:
ruby-core #89822
Issue #15144 has been updated by knu (Akinori MUSHA).
Status changed from Open to Assigned
Assignee set to matz (Yukihiro Matsumoto)
I'm working on this and the implementation of Enumerator#+(enum) and Enumerator#chain(*enums) is about to complete.
Matz, what do you think about the name "chain"? Python has [chain()](https://docs.python.org/3/library/itertools.html#itertools.chain), Rust too has [chain](https://doc.rust-lang.org/core/iter/trait.Iterator.html#method.chain), and I cannot think of any better name.
----------------------------------------
Feature #15144: Enumerator#chain
https://bugs.ruby-lang.org/issues/15144#change-74882
* Author: zverok (Victor Shepelev)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version:
----------------------------------------
I am not sure I am not missing something, but...
```ruby
[1, 2, 3].each.chain([3, 4, 5].each) # => Enumerator
```
...seem to be a useful pattern.
It especially shows itself in case of lazy enumerators, representing several long-calculated sequences, like something...
```ruby
# just data from several sources, abstracted into enumerator, fetching it on demand
process = URLS.lazy.map(&Faraday.method(:get)))
.chain(LOCAL_FILES.lazy.map(&File.method(:read)))
.chain(FALLBACK_FILE.then.lazy.map(&File.method(:read))) # with yield_self aka then we can even chain ONE value
process.detect { |val| found?(val) } # uniformely search several sources (lazy-loading them) for some value
# tty-progressbar is able to work with enumerables:
bar = TTY::ProgressBar.new("[:bar]", total: URLS.count + LOCAL_FILES.count + 1)
bar.iterate(process).detect { |val| found?(val) } # shows progress-bar for uniform process of detection
```
Prototype impl. is dead simple, of course:
```ruby
class Enumerator
def chain(*others)
Enumerator.new { |y|
[self, *others].each { |e| e.each { |v| y << v } }
}
end
end
```
Obviously, the effect could be reached with `flat_map`, but it seems "chaining" of iterations is pretty common and clear concept (and Google search for "ruby enumerator chain" shows people constantly ask about the way).
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>