[Newbies] How to empty a collection?

goran at krampe.se goran at krampe.se
Tue Feb 19 17:01:09 UTC 2008


merlyn at stonehenge.com (Randal L. Schwartz) wrote:
> >>>>> "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:
> 
>     removeAll
>         self notEmpty ifTrue: [self become: self species new].
> 
> and let subclasses make it more efficient using implementation knowledge.

I would never use become: in "regular code" like this. :)

regards, Göran


More information about the Beginners mailing list