. .


ST 4U 332: Bags and Sets

January 16, 2013 8:20:20.783

Today's Smalltalk 4 You looks at two collection classes, Bag and Set. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:


If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:

Two of the Collection classes that you might need to know more about are Set and bag. The best way to look at that is via a small example:

| collection c1 |
collection := #(1 1 2 3 4 5 6 6 6 7 8 0 9 9).
c1 := collection asBag.
c1 inspect

Inspecting that gives you this:


What a Bag does is tell you the count for each unique element in the collection. That can be useful if you don't care about the order, but are interested in the occurrences - like this:

c1 occurrencesOf: 9.

That should give you 2.

Now let's try a Set:

| collection c1 |
collection := #(1 1 2 3 4 5 6 6 6 7 8 0 9 9).
c1 := collection asSet.
c1 inspect

Inspect that, and you'll see:


Sets give you just the unique elements - useful if you want to filter out duplicates.

Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Tags: ,

[st4u332-iPhone.m4v ( Size: 2738489 )]

posted by James Robertson

 Share Tweet This