. .


ST 4U 315: Writing Collections with Streams

November 30, 2012 10:09:51.520

Today's Smalltalk 4 You looks at using Streams to write collections (not Strings). 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:

Write 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 have a look at writing arbitrary collections via streams. In Smalltalk, streams can be used to write collections of objects (not just strings). For example: here we set up a ByteArray, and then write to it:

collection := ByteArray new: 5.
stream := collection writeStream.
stream nextPut: 10.
stream nextPut: 55.
stream nextPut: 10000.

If you inspect the results, you'll see the ByteArray #[10 55 0 0 0]. You can also create collections of disparate objects:

collection2 := Array new: 5.
stream2 := collection2 writeStream.
stream2 nextPut: 'one'.
stream2 nextPut: (1/3).

Inspecting the results will give you the collection #('one' (1/3)) - the last object, if you look in the inspector, is a fraction.

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.

Technorati Tags: ,

[st4u315-iPhone.m4v ( Size: 2693564 )]

posted by James Robertson

 Share Tweet This