[squeak-dev] Question About Collections - SOLVED

JohnReed Maffeo aldeveron at graffiti.net
Tue Jan 7 02:44:06 UTC 2014


> ----- Original Message -----
> From: tim Rowledge
> Sent: 01/06/14 02:37 PM
> To: The general-purpose Squeak developers list
> Subject: Re: [squeak-dev] Question About Collections
> 
> 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

Dictionary is the solution that worked for me. Starting with the Ordered collection sorted in title, date order.

compressSchedule
 "Compress the schedule by removing multiple entries for a single title (shows
 are broadcast more than once"
 
 | aDictionary temp anOrderedCollection |
 
 aDictionary := Dictionary new.
 temp := aBBCRadioSchedule collect: [:each | aDictionary at: each title put: each].
 anOrderedCollection := OrderedCollection new.
 aDictionary valuesDo: [ :each | anOrderedCollection add: each].
 ^ aBBCRadioSchedule := anOrderedCollection.

This may need to be refactored a bit, but it works.

And with that I have the alpha version of my program running and I have 
finally learned the rudiments of GUI building using Morphs.

Thanks,

jrm



More information about the Squeak-dev mailing list