From: "ara.t.howard" Date: 2007-10-26T05:48:05+09:00 Subject: Re: Intervals in Ruby On Oct 25, 2007, at 4:00 AM, Steven D'Aprano wrote: > Are there many (any?) good examples of where you need to iterate > over an > integer count, rather than directly over a data structure? > yes. the most obvious one is needing adjacent entries and/or self- modification. something along the lines of pixels.size.times do |i| pixel = pixels[i] left = pixels[i - 1] right = pixels[i + 1] if left.nil? and condition(right) or condition(left) and condition(right) or condition(left) and right.nil? pixel = transformation left, pixel, right end end granted, i personally would create my own neighborhood iterator for such a case - but then sometimes you need to compare adjacent neighborhoods... i would go so far as to say *any* interesting algorithm can require random access into a data structure that's also being modified. in those cases iterating by size is a smart alternative since very few data structures in ruby support modification + traversal. another good use case is min = [a.size, b.size].min min.times do |i| something_with a[i] and b[i] end which is to say iterating datasets in parallel. yes there is zip - fantastic if you are into duplicating both datasets in memory *and* are iterating over n=2 data structures but otherwise useless. that said - i'll take #each or #map any day. cheers. a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama