Internal iterators in ruby

Hi,

I read that it is difficult to iterate over two
collections in paralell using ruby's internal
iterators.
can any one explain me indetail why it is so..

thanks in advance

Navya

···

__________________________________
Discover Yahoo!
Get on-the-go sports scores, stock quotes, news and more. Check it out!
http://discover.yahoo.com/mobile.html

Hi,

I read that it is difficult to iterate over two
collections in paralell using ruby's internal
iterators.
can any one explain me indetail why it is so..

It may not be.

left = [1, 2, 3, 4]
right = %w[one two three four]

left.zip(right) {|l,r| puts "#{l} = #{r}"}

thanks in advance

Navya

E

···

Le 2/6/2005, "Navya Amerineni" <navyaamerineni@yahoo.com> a écrit:

--
template<typename duck>
void quack(duck& d) { d.quack(); }

ES wrote:

It may not be.

left = [1, 2, 3, 4]
right = %w[one two three four]

left.zip(right) {|l,r| puts "#{l} = #{r}"}

That said, it can be tricky to implement zip in Ruby, and I'm guessing the use of continuations is required.

LOGIC The principle governing human intelligence. Its nature may be deduced from examining the two following propositions, both of which are held by human beings to be true and often by the same people: "I can't so you mustn't," and "I can but you mustn't."
    - The Hipcrime Vocab by Chad C. Mulligan

I do not follow, that is Array#zip. In any case, it is quite possible
to implement zip without continuations -- see Enumerable#zip.

Did you mean something else instead?

E

···

Le 2/6/2005, "Nicholas Seckar" <nseckar@gmail.com> a écrit:

ES wrote:

It may not be.

left = [1, 2, 3, 4]
right = %w[one two three four]

left.zip(right) {|l,r| puts "#{l} = #{r}"}

That said, it can be tricky to implement zip in Ruby, and I'm guessing
the use of continuations is required.

--
template<typename duck>
void quack(duck& d) { d.quack(); }

Array#zip is built-in, but its not hard to implement.

From MetaRuby (so it is written in a funky style and could be much shorter):

class Array
   def zip(*args)
     raise "I don't do that yet" if block_given?

     args = args.map { |a| convert a }
     args_len = args.length

     len = self.length
     result = Array.new len

     0.upto(length - 1) do |i|
       tmp = Array.new args_len + 1
       tmp[0] = self.at(i)

       0.upto(args_len - 1) do |j|
         tmp[j + 1] = args[j][i]
       end

       # I think you could make it support blocks here with yield(*tmp)
       result[i] = tmp
     end

     return result
   end

   private

   def convert(object)
     unless object.respond_to? :to_ary then
       raise TypeError, "cannot convert " + object.class.name + " into Array"
     end
     return object.to_ary
   end

end

(It would support blocks if I had tests for them, but they are missing from rubicon.)

···

On 01 Jun 2005, at 21:05, Nicholas Seckar wrote:

ES wrote:

It may not be.

left = [1, 2, 3, 4]
right = %w[one two three four]

left.zip(right) {|l,r| puts "#{l} = #{r}"}

That said, it can be tricky to implement zip in Ruby, and I'm guessing the use of continuations is required.

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

It's only hard if you want to avoid reifying the enumerable as array.

···

On Thursday 02 June 2005 12:49 am, Eric Hodel wrote:

> That said, it can be tricky to implement zip in Ruby, and I'm
> guessing the use of continuations is required.

Array#zip is built-in, but its not hard to implement.

--
-- Jim Weirich jim@weirichhouse.org http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)