summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-enumerator/mrblib/enumerator.rb
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2019-09-12 22:41:33 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2019-09-13 02:28:56 +0900
commitf5542b992731f4d1af1505837a71e782decab25f (patch)
treec9de1c20abef942ebddc1bf6bae34ca05dfcae3c /mrbgems/mruby-enumerator/mrblib/enumerator.rb
parentd4af765651193d342488cfbbf81f352f53d5377e (diff)
downloadmruby-f5542b992731f4d1af1505837a71e782decab25f.tar.gz
mruby-f5542b992731f4d1af1505837a71e782decab25f.zip
Add `Enumerator.produce` from Ruby2.7
Diffstat (limited to 'mrbgems/mruby-enumerator/mrblib/enumerator.rb')
-rw-r--r--mrbgems/mruby-enumerator/mrblib/enumerator.rb36
1 files changed, 36 insertions, 0 deletions
diff --git a/mrbgems/mruby-enumerator/mrblib/enumerator.rb b/mrbgems/mruby-enumerator/mrblib/enumerator.rb
index 89472ef01..d02caf5a0 100644
--- a/mrbgems/mruby-enumerator/mrblib/enumerator.rb
+++ b/mrbgems/mruby-enumerator/mrblib/enumerator.rb
@@ -556,6 +556,42 @@ class Enumerator
self
end
end
+
+ ##
+ # call-seq:
+ # Enumerator.produce(initial = nil) { |val| } -> enumerator
+ #
+ # Creates an infinite enumerator from any block, just called over and
+ # over. Result of the previous iteration is passed to the next one.
+ # If +initial+ is provided, it is passed to the first iteration, and
+ # becomes the first element of the enumerator; if it is not provided,
+ # first iteration receives +nil+, and its result becomes first
+ # element of the iterator.
+ #
+ # Raising StopIteration from the block stops an iteration.
+ #
+ # Examples of usage:
+ #
+ # Enumerator.produce(1, &:succ) # => enumerator of 1, 2, 3, 4, ....
+ #
+ # Enumerator.produce { rand(10) } # => infinite random number sequence
+ #
+ # ancestors = Enumerator.produce(node) { |prev| node = prev.parent or raise StopIteration }
+ # enclosing_section = ancestors.find { |n| n.type == :section }
+ def Enumerator.produce(init=NONE, &block)
+ raise ArgumentError, "no block given" if block.nil?
+ Enumerator.new do |y|
+ if init == NONE
+ val = nil
+ else
+ val = init
+ y.yield(val)
+ end
+ loop do
+ y.yield(val = block.call(val))
+ end
+ end
+ end
end
module Kernel