Bug in Array.zip

This email from the ruby vs python discussion points out a bug in
Array.zip

p=Prime.new
=> #<Prime:0x2b43048 @primes=, @seed=1, @counts=>
for i in [1,2,3].zip(p)
p i
end
TypeError: cannot convert Prime into Array
from (irb):5:in `zip’
from (irb):5
p.kind_of?Enumerable
=> true

guess why you can’t use a prime? because you can’t pause the
yielding. You’re forced to use the whole number of Prime
number that are actually… infinite. So, you discover that
you can’t zip it cause zip calls to_ary.

The following is a reimplementation of zip that does not use an
intermediate array. Performance-wise, it will probably suck due to the
use of continuations - perhaps the code in Array.zip should fall back to
something like this if it fails to convert the passed argument into an
array?

class Array
def zip(otherArray)
normalFlow, nextIter, value = nil
callcc { |normalFlow|
callcc { |nextIter| normalFlow.call }
otherArray.each { |x|
value = x
callcc { |nextIter| normalFlow.call }
}
nextIter = nil
normalFlow.call
}
collect { |first|
[ first,
if nextIter
callcc { |normalFlow| nextIter.call }
value
end
]
}
end
end

(0…20).to_a.zip(Prime.new).each { |i|
p i
}

···

#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################