-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement missing Iterator::SingletonProc#rewind #3795
Implement missing Iterator::SingletonProc#rewind #3795
Conversation
For example: a = b = 1
fib = Iterator.of do
a, b = b, a
a = a + b
b
end
p fib.next # => 1
p fib.next # => 2
p fib.next # => 3
p fib.next # => 5
p fib.next # => 8
fib.rewind # ??? We can't rewind because there's no way to know the initial state of the iterator, which forms a closure. So |
@asterite Yes, I know all you said. Though I think At first, I thought we should raise a runtime exception when calling lines = STDIN.each_line
p lines.first(2).to_a
lines.rewind.each do |line|
p line
end So, I decide to return itself by |
I think Maybe |
I think it is better than now 😄 |
@asterite In fact, |
#codetriage
As I understand it, option 2 was considered more favourable and I'd support that move. It's better to fail loudly instead of continuing silently without notice. |
There's a third possibility. In other languages an I added I was surprised to see this code working fine in Ruby: p STDIN.each_line.first(3).cycle(10).to_a How can that work?? Well, turns out that their
I think that's acceptable. When you cycle over elements they are usually not that many. And if they are, well, at least they will not be infinite (because what would be the point of I just tried it out and it works really well (including that snippet from Ruby). I don't think rewinding iterators is that common, or actually needed. Another advantage is that it simplifies implementing an What do you think? Should I create a separate issue/PR to discuss this? |
Closed because in #7440 we removed |
@asterite Why add this to the milestone? Also, is this really a bug? |
oops |
No description provided.