internal iterators in ruby

  • Thread starter Navya Amerineni
  • Start date
N

Navya Amerineni

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
 
E

ES

Le 2/6/2005 said:
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 =3D [1, 2, 3, 4]
right =3D %w[one two three four]

left.zip(right) {|l,r| puts "#{l} =3D #{r}"}
thanks in advance

Navya

E
 
N

Nicholas Seckar

ES said:
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
 
E

ES

Le 2/6/2005 said:
ES said:
It may not be.

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

left.zip(right) {|l,r| puts "#{l} =3D #{r}"}
That said, it can be tricky to implement zip in Ruby, and I'm guessing
the use of continuations is required.

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
 
E

Eric Hodel

ES said:
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.

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]
end

# I think you could make it support blocks here with yield(*tmp)
result = 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.)
 

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

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,478
Latest member
ReginaldVi

Latest Threads

Top