[Newbies] Re: Deleting elements from a Collection while looping through it

Jerome Peace peace_the_dreamer at yahoo.com
Sun Dec 11 01:37:44 UTC 2011


Are your falling blocks unique? Is there a reason you are putting them into a Set instead of some other collection?

One of the hidden features of Squeak/Smalltalk is that stack like collections exist. Because OrderedCollections have both a add/removeFirst and add/removeLast methods they can easily act like a stack or a queue. If you want to loop thru a queue of falling bricks just remove each brick and replace the still falling ones at the end. No?

A Set would only be useful if you are mapping many of something onto one of the same something and you have a lot of different somethings. That doesn't sound like what you are doing.

If you have a method called say #falling which when sent to a brick causes it to fall and return true or causes it to not fall and return false then a simple:

bricks := (1 to: 100) collect: [:each | Brick new ].

bricks := bricks asOrderedCollection . "Optional"

bricks := bricks select: #falling.

the above is short for 

bricks := bricks select: [:each | each falling ] .

would have each brick do its thing and store the list of still falling bricks in the variable.

Yours in curiosity and service, Jerome Peace

More information about the Beginners mailing list