. .

st4u

ST 4U 354: Collections and Streams

March 8, 2013 9:00:18.451

Today's Smalltalk 4 You looks at collection limitations, and how they can be bypassed using streams. 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:

Collections and Streams.

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:


Today we'll take a look at streams and collections. We've pointed out before that streaming works across any collection (not just strings) - but streaming also "bypasses" some of the limitations built into some of the collection classes. Take an Array, for instance. Being fixed size, this will fail:


array := #(1 2 3 4).
array add: 5.

Normally, that's not an issue - you probably picked an Array because you had a fixed set of objects, and didn't need to grow it. But what if you did? Well, you can use a stream:


array := #(1 2 3 4).
stream := ReadWriteStream on: array.
stream upToEnd.
stream nextPut: 5.
^stream contents.

If you inspect the results, you'll see that you still have an Array, now with 5 objects rather than 4. It's actually a copy of the initial array, not the same one grown. You could, of course, accomplish the same thing via the transformations APIs:


array := #(1 2 3 4).
oc := array asOrderedCollection.
oc add: 5.
^oc

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: , ,

Enclosures:
[st4u354-iPhone.m4v ( Size: 1060479 )]

posted by James Robertson

Comments

Re: ST 4U 354: Collections and Streams

[RandalSchwartz] March 8, 2013 21:26:26.569

Not sure why you didn't just use:

array := #(1 2 3 4).

newArray = array , 5.

The comma method knows how to do all that copy-as-same-type stuff.

Re: ST 4U 354: Collections and Streams

[RandalSchwartz] March 8, 2013 21:28:59.963

Ooops. newarray := array , #(5).

 Share Tweet This