Sirs,
As a trial project I worked on the Path through Net problem, where I use a Point and Vector model. At some point I encountered a strange situation: the following code did not work correctly for one of vectors in the helpSet, though I did test the situation thoroughly and endPoint is postively in pointsOnPath :
helpSet do: [:vector | ( pointsOnPath includes: (vector endPoint )) ifTrue: [ helpSet remove: vector ifAbsent: [^'Error 2, class NetClass'] ]. ].
But once I applied the following code on the same data:
helpSet := helpSet reject: [:vector | pointsOnPath includes: (vector endPoint )].
the result was correct.
I am new to ST and can't find my mistake. Any help is appreciated.
LP Jaril Lavrenčič
Jaril diris@volja.net asked about a strange effect:
As a trial project I worked on the Path through Net problem, where I use a Point and Vector model. At some point I encountered a strange situation:
the
following code did not work correctly for one of vectors in the helpSet, though I did test the situation thoroughly and endPoint is postively in pointsOnPath :
helpSet do: [:vector | ( pointsOnPath includes: (vector endPoint )) ifTrue: [ helpSet remove: vector ifAbsent: [^'Error 2, class
NetClass'] ]. ].
Your mistake is that you remove an element from a set while you enumerate its elements with #do: That is not allowed, or better, it creates erroneous effects. (Removal of elements rearranges the internal structure of a collection. The #do:-method uses a counter that is simply incremented. When the internal structure is reearranged you may occasionally skip an element.)
The solution with reject is save, because reject adds the "survivers" (the elements that do not satisfy the rejection condition) into a new set. The set that you scan with #do: is left unchanged.
Hope that helps, Boris
On Sunday 01 December 2002 09:16 am, Jaril wrote:
I am new to ST and can't find my mistake. Any help is appreciated.
You're trying to modify a collection while iterating through it. This generally doesn't work.
squeak-dev@lists.squeakfoundation.org