Actions
Bug #20623
closedUnexpected behavior of blocks in Enumerator::Lazy#zip
Description
I've stumbled upon unexpected behaviour of lazy enumerators. My example:
[1].cycle.lazy.zip([2].cycle.lazy).map do |x|
puts x.inspect
end.take(5).force
puts "---"
[1].cycle.lazy.zip([2].cycle.lazy).take(5).force.map do |x|
puts x.inspect
end
The outputs (and variable assignment inside the block) differs
1
1
1
1
1
---
[1, 2]
[1, 2]
[1, 2]
[1, 2]
[1, 2]
Updated by esad (Esad Hajdarevic) 6 months ago
- Subject changed from Unexpected behavior of blocks in LazyEnumerator#map to Unexpected behavior of blocks in LazyEnumerator#zip
Updated by esad (Esad Hajdarevic) 6 months ago
- Subject changed from Unexpected behavior of blocks in LazyEnumerator#zip to Unexpected behavior of blocks in Enumerator::Lazy#zip
Updated by eileencodes (Eileen Uchitelle) 6 months ago
I tested this against a few versions of Ruby. In 2.7.x the first example returns [1,2]
. But since 3.0.x it returns 1
. It also seems cycle
isn't needed to reproduce it, so I removed that from the reproduction script. I wasn't able to get it any smaller, as lazy
and force
(or to_a
) is required.
[1].lazy.zip([2].lazy).map do |x|
puts x.inspect
end.take(5).force
puts "---"
[1].lazy.zip([2].lazy).take(5).force.map do |x|
puts x.inspect
end
Updated by jeremyevans0 (Jeremy Evans) 5 months ago
I submitted a pull request to fix this: https://github.com/ruby/ruby/pull/11212
Updated by jeremyevans0 (Jeremy Evans) 4 months ago
- Status changed from Open to Closed
Actions
Like0
Like0Like0Like0Like0Like0