container stuff

bearophile bearophileHUGS at
Thu May 27 05:02:46 PDT 2010

> When is it better to do it that way, rather than just iterating over all 
> elements, and then completely empty the container?
> (Just curious -- I'm having trouble thinking of a use case for this 
> feature).

I'm having troubles understanding why two persons have troubles seeing use cases for this feature :-)

Iterating over the container and then emptying the container is two operations, you have to keep in mind to empty it, while if you pop items out of it progressively you just need to keep in mind to do one thing, and you avoid forgetting the final cleaning.

Also, the progressive popping out of items allows you to have a collection that is correct in every moment, there is no risk of "removing" two times an item, so you can pass around the data structure in any moment of this process of wearing it away.

This is what you suggest (Python code):

s = set(["ab", "bc", "de"])

def process_item(item):
    print item

for item in s:
s.clear() # removes all items

But this is better, you can print the correct collection in any moment and you can't forget the final clear:

s = set(["ab", "bc", "de"])

def process_item(item):
    print item

def show_data(items):
    print items

while s:


More information about the Digitalmars-d mailing list