Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
f5542b99
Commit
f5542b99
authored
Sep 12, 2019
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add `Enumerator.produce` from Ruby2.7
parent
d4af7656
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
1 deletion
+75
-1
mrbgems/mruby-enumerator/mrblib/enumerator.rb
mrbgems/mruby-enumerator/mrblib/enumerator.rb
+36
-0
mrbgems/mruby-enumerator/test/enumerator.rb
mrbgems/mruby-enumerator/test/enumerator.rb
+39
-1
No files found.
mrbgems/mruby-enumerator/mrblib/enumerator.rb
View file @
f5542b99
...
...
@@ -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
...
...
mrbgems/mruby-enumerator/test/enumerator.rb
View file @
f5542b99
...
...
@@ -10,9 +10,9 @@ def assert_take(exp, enumerator)
result
=
[]
n
=
exp
.
size
enumerator
.
each
do
|
v
|
break
if
n
==
0
result
<<
v
n
-=
1
break
if
n
==
0
end
assert_equal
exp
,
result
end
...
...
@@ -560,3 +560,41 @@ assert 'Enumerable#zip' do
assert_raise
(
TypeError
)
{
[
1
].
zip
(
1
)
}
end
assert
'Enumerator.produce'
do
assert_raise
(
ArgumentError
)
{
Enumerator
.
produce
}
# Without initial object
passed_args
=
[]
enum
=
Enumerator
.
produce
{
|
obj
|
passed_args
<<
obj
;
(
obj
||
0
).
succ
}
assert_equal
Enumerator
,
enum
.
class
assert_take
[
1
,
2
,
3
],
enum
assert_equal
[
nil
,
1
,
2
],
passed_args
# With initial object
passed_args
=
[]
enum
=
Enumerator
.
produce
(
1
)
{
|
obj
|
passed_args
<<
obj
;
obj
.
succ
}
assert_take
[
1
,
2
,
3
],
enum
assert_equal
[
1
,
2
],
passed_args
# Raising StopIteration
words
=
%w[The quick brown fox jumps over the lazy dog]
enum
=
Enumerator
.
produce
{
words
.
shift
or
raise
StopIteration
}
assert_equal
%w[The quick brown fox jumps over the lazy dog]
,
enum
.
to_a
# Raising StopIteration
object
=
[[[[
"abc"
,
"def"
],
"ghi"
,
"jkl"
],
"mno"
,
"pqr"
],
"stuv"
,
"wxyz"
]
enum
=
Enumerator
.
produce
(
object
)
{
|
obj
|
obj
.
respond_to?
(
:first
)
or
raise
StopIteration
obj
.
first
}
assert_nothing_raised
{
assert_equal
[
[[[[
"abc"
,
"def"
],
"ghi"
,
"jkl"
],
"mno"
,
"pqr"
],
"stuv"
,
"wxyz"
],
[[[
"abc"
,
"def"
],
"ghi"
,
"jkl"
],
"mno"
,
"pqr"
],
[[
"abc"
,
"def"
],
"ghi"
,
"jkl"
],
[
"abc"
,
"def"
],
"abc"
,
],
enum
.
to_a
}
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment