13.07.2015 Views

Smalltalk Best Practice Patterns Volume 1: Coding - Free

Smalltalk Best Practice Patterns Volume 1: Coding - Free

Smalltalk Best Practice Patterns Volume 1: Coding - Free

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Duplicate Removing SetYou have a Collection.How do you remove the duplicates from a Collection?I remember answering this question on a test in school. I know I’ve seen the C-ish code for it whilereviewing code. It always looks ugly:unique := OrderedCollection new.self owners do: [:each | (unique includes: each) ifFalse: [unique add: each]]No need for that in <strong>Smalltalk</strong>. Once you get over the idea that allocating memory is expensive inprogramming or execution time, and once you get used to the power of the Collection classes,problems like this become trivial.Send “asSet” to the Collection. The result will have all duplicates removed.I sure wish I’d been using <strong>Smalltalk</strong> when I was in college!When you tune performance, you may find that you are allocating Collections for the sameelements over and over and over. Composing all those intermediate Collections together is no bigdeal. Remember, you’re going for clarity of expression.Use a Temporarily Sorted Collection if you need the unique elements sorted.<strong>Coding</strong> <strong>Patterns</strong> page 112 of 147 9/30/2006

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!