[squeak-dev] Question About Collections

tim Rowledge tim at rowledge.org
Mon Jan 6 21:37:11 UTC 2014


On 06-01-2014, at 12:56 PM, JohnReed Maffeo <aldeveron at graffiti.net> wrote:

> I havea collection of radio program schedule objects which includes title and broadcast date.
> 
> (title, date)is unique, but title is not, because radio shows can be repeated. I
> just need a list with the title and the last broadcast date.
> 
> What is the Smalltalk way of creating a new collection where title is unique? I know
> how to do this in procedural code, but I expect that there is a more elegant, OO way.
> 
> OrderedCollection
> 
> Dr Who  1/1/14
> Dr Who  1/2/14
> Dr Who  1/3/14
> 
> Result 
> Dr Who  1/3/14

Looks like a simple use of either a Set or Dictionary, depending on what you want to do next. I’ll assume a Dictionary so you can retrieve your info later for a variety of shows -
showList := Dictionary new.

thisShow := TVShow named: ‘Dr Who’ details: ‘blahblahblah’.
showList at: thisShow name put: thisShow.

thisShow := TVShow named: ‘Dr Who’ details: ‘blahblahblah2’.
showList at: thisShow name put: thisShow.

thisShow := TVShow named: ‘Dr Who’ details: ‘blahblahblah3’.
showList at: thisShow name put: thisShow.

This will result in a list where the last Dr Who is the only one there since you’ve effectively replaced the earlier ones. So, if you wanted to make sure only the latest dated item were left you’d have to test the dates.

((showList at: thisShow name) broadcastDate < thisShow broadcastDate) ifTrue:[showList at: thisShow name put: thisShow]

would do that. However, there must be nicer databasey classes that someone has written (Hint: Magma) that would do so much better.


tim
--
tim Rowledge; tim at rowledge.org; http://www.rowledge.org/tim
Oxymorons: Microsoft Works




More information about the Squeak-dev mailing list