Question About Collections

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Question About Collections

JohnReed Maffeo
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

Thanks,
jrm

Reply | Threaded
Open this post in threaded view
|

RE: Question About Collections

Ron Teitelbaum
Hi JRM,

How about

RadioProgramSchedule
        iv: 'programs'

initialize
        programs := Dictionary new.

addShow: aShowName airsOn: aDate

        self programs at: aShowName ifAbsentPut: [SortedCollection new].
        (self programs at: aShowName) add: aDate.
       
With this you end up with

Dictionary
        Dr. Who
                SortedCollection
                        1/1/2014
                        1/2/2014
  1/3/2014
        The Who
                SortedCollection
                        2/1/1985
        Horton Hears a Who
                ....

        But this will also allow you to add the same date twice.  You could use set instead of SortedCollection or check for the date first before adding it.

All the best,

Ron Teitelbaum
Head Of Engineering
3d Immersive Collaboration Consulting
[hidden email]
Follow Me On Twitter: @RonTeitelbaum
www.3dicc.com
https://www.google.com/+3dicc


> -----Original Message-----
> From: [hidden email] [mailto:squeak-dev-
> [hidden email]] On Behalf Of JohnReed Maffeo
> Sent: Monday, January 06, 2014 3:56 PM
> To: The general-purpose Squeak developers list
> Subject: [squeak-dev] Question About Collections
>
> 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
>
> Thanks,
> jrm
>



Reply | Threaded
Open this post in threaded view
|

Re: Question About Collections

timrowledge
In reply to this post by JohnReed Maffeo

On 06-01-2014, at 12:56 PM, JohnReed Maffeo <[hidden email]> 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; [hidden email]; http://www.rowledge.org/tim
Oxymorons: Microsoft Works