[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

internal iterators in ruby

Navya Amerineni

6/2/2005 3:39:00 AM

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/m...


5 Answers

ES

6/2/2005 3:46:00 AM

0


Le 2/6/2005, "Navya Amerineni" <navyaamerineni@yahoo.com> a écrit:
>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


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


Nicholas Seckar

6/2/2005 4:06:00 AM

0

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


ES

6/2/2005 4:47:00 AM

0


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.

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

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


Eric Hodel

6/2/2005 4:49:00 AM

0

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.

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.)

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



Jim Weirich

6/2/2005 6:53:00 AM

0

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.

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

--
-- Jim Weirich jim@weirichhouse.org http://onest...
-----------------------------------------------------------------
"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)