From: "ara.t.howard" Date: 2007-10-26T08:11:20+09:00 Subject: Re: Intervals in Ruby On Oct 25, 2007, at 3:58 PM, mortee wrote: > What about #each_with_index for this case? Using pixels.size.times and > accessing each element by the yielded integer indexes explicitely > assumes that the index space is continuous - however, the same > algorithm > using #each_with_index would only assume that each item yielded by it > has two adjacent neighbors. well, frankly, that example is made up but i have tons of image processing code that does that in spirit - the point is that using #each_with_index is going to give you errors: you cannot iterate using map/each/each_with_index *while* also modifying a datastructure - try this: cfp:~ > ruby -e' (a=[42]).each_with_index{ a << 42 } ' ... it will never exit. i certainly abstract that kind of code to yield the pixel and it's neighbors (it doesn't always have them near the edges of an image) but abstracting it will require both iteration by size *and* using random access unless the algorithm is read only. you can see http:// kogs-www.informatik.uni-hamburg.de/~koethe/vigra/ for example of how neighborhood iterators can generally be abstracted - my point is just that the this *type* of problem - iterating while modifying and looking at neighbors - requires an external iterator if one is using the 'obvious' solution. 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