Skip to content

Commit 4e928a9

Browse files
committed
[GR-19220] Implement Enumerator#produce (#2160)
PullRequest: truffleruby/2166
2 parents 001f1dc + 47db835 commit 4e928a9

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Compatibility:
1717
* Implement `Enumerable#tally` and `Enumerable#filter_map` (#2144 and #2152, @LillianZ).
1818
* Implement `Range#minmax`.
1919
* Pass more `Enumerator::Lazy#uniq` and `Enumerator::Lazy#chunk` specs (#2146, @LillianZ).
20+
* Implement `Enumerator#produce` (#2160, @zverok)
2021

2122
Performance:
2223

spec/tags/core/enumerator/produce_tags.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/main/ruby/truffleruby/core/enumerator.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,19 @@ def +(other)
216216
Enumerator::Chain.new(self, other)
217217
end
218218

219+
def self.produce(initial = nil)
220+
# Taken from https://github.com/zverok/enumerator_generate
221+
raise ArgumentError, 'No block given' unless block_given?
222+
Enumerator.new(Float::INFINITY) do |y|
223+
val = initial == nil ? yield() : initial
224+
225+
loop do
226+
y << val
227+
val = yield(val)
228+
end
229+
end
230+
end
231+
219232
class Yielder
220233
def initialize(&block)
221234
raise LocalJumpError, 'Expected a block to be given' unless block_given?

0 commit comments

Comments
 (0)