From: mortee Date: 2007-10-26T10:49:22+09:00 Subject: Re: Intervals in Ruby ara.t.howard wrote: > 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. Fair enough. However, the code in question hasn't altered the array of pixels itself, just the pixel values. It's true that you have to pay more attention when adding and/or deleting items while iterating. mortee