Bug #7691

3 bugs with Lazy enumerators with state

Added by Marc-Andre Lafortune over 1 year ago. Updated about 1 year ago.

[ruby-core:51420]
Status:Closed
Priority:High
Assignee:-
Category:core
Target version:2.0.0
ruby -v:r38800 Backport:

Description

I found the following 3 bugs with Lazy#drop, drop_while and zip:

drop = (1..4).lazy.drop(2)
drop.to_a # => [3, 4]
drop.to_a # => [1, 2, 3, 4], should be same as above

drop_while = (1..4).lazy.drop_while(&:odd?)
drop_while.to_a # => [2, 3, 4]
drop_while.to_a # => [1, 2, 3, 4], should be same as above

zip = (1..2).lazy.zip([3, 4])  # or (3..4)
zip.to_a # => [[1, 3], [2, 4]]
zip.to_a # => [[1, nil], [2, nil]] should be same as above

I found them when writing a Ruby implementation of Enumerator::Lazy.

My implementation created the same bug with #take as described in https://bugs.ruby-lang.org/issues/6428.

This made me realize that the api of Lazy.new makes it near impossible to write most lazy enumerators requiring a state, as there is no general way to reset that state.

When looking at my code, I used a state for drop, drop_while and zip. A quick verification showed me that the MRI implementation also has the same bugs!gene

So the meta question is: should there not be a general way to initialize the state of the lazy enum?


Related issues

Related to ruby-trunk - Bug #7696: Lazy enumerators with state can't be rewound Closed 01/15/2013
Related to ruby-trunk - Bug #6428: lazy版take/dropに2度forceを呼んだ時の挙動 Closed 05/13/2012

Associated revisions

Revision 38921
Added by Marc-Andre Lafortune about 1 year ago

  • enumerator.c: Fix state handling for Lazy#drop_while
    [bug #7696] [bug #7691]

  • test/ruby/testlazyenumerator.rb: test for above

Revision 38922
Added by Marc-Andre Lafortune about 1 year ago

  • enumerator.c: Fix state handling for Lazy#drop
    [bug #7696] [bug #7691]

  • test/ruby/testlazyenumerator.rb: test for above

Revision 38923
Added by Marc-Andre Lafortune about 1 year ago

  • enumerator.c: Fix state handling for Lazy#zip
    [bug #7696] [bug #7691]

  • test/ruby/testlazyenumerator.rb: test for above

History

#1 Updated by Marc-Andre Lafortune about 1 year ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r38921.
Marc-Andre, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • enumerator.c: Fix state handling for Lazy#drop_while
    [bug #7696] [bug #7691]

  • test/ruby/testlazyenumerator.rb: test for above

Also available in: Atom PDF