[Newbies] How to empty a collection?

Randal L. Schwartz merlyn at stonehenge.com
Tue Feb 19 15:02:10 UTC 2008

>>>>> "Brian" == Brian Murphy-Dye <brian at murphydye.com> writes:

Brian> My understanding as well is that this is the purpose of #become:, but
Brian> also that it is not very efficient in some versions of Smalltalk. If
Brian> you are using an OrderedCollection, this is probably a reasonably
Brian> efficient method as it doesn't need to search through the whole list
Brian> every time it removes an element:

Brian> removeAll
Brian>     [self notEmpty] whileTrue: [self removeLast]

What makes you think #removeLast doesn't need to search through the whole list
in some kinds of OrderedCollection?  You *do* have an implementation
assumption there.

I'd just do:

        self notEmpty ifTrue: [self become: self species new].

and let subclasses make it more efficient using implementation knowledge.

Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

More information about the Beginners mailing list