Bug in Array.zip

  • Thread starter Daniel Sheppard
  • Start date
D

Daniel Sheppard

This email from the ruby vs python discussion points out a bug in
Array.zip
p=Prime.new
=> # said:
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=> 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.
#####################################################################################
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,369
Latest member
FTMZ

Latest Threads

Top