Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
An interesting exercise is to create a very large bag <strong><strong>an</strong>d</strong> <strong>an</strong> array (e.g. over 100,000 numbers) <strong><strong>an</strong>d</strong> time<br />
the perform<strong>an</strong>ce of the inject: message versus the do: message on the two objects. The result of doing<br />
this for bags <strong><strong>an</strong>d</strong> arrays of 100, 1000, 10000, 100000 <strong><strong>an</strong>d</strong> 1,000,000 is interesting. The perform<strong>an</strong>ce of<br />
the array is noticeably faster, however the differences between the inject: <strong><strong>an</strong>d</strong> do: messages (for the<br />
same class) are negligible.<br />
The <strong>Smalltalk</strong> facility which allows the developer to time their code is millisecondsToRun:.<br />
This is used to calculate the processor time taken to execute the code in the block passed to it. For<br />
example:<br />
| <strong>an</strong>Array time1 aNumber |<br />
<strong>an</strong>Array := Array new: 10000000.<br />
1 to: 10000000 do: [:i | <strong>an</strong>Array at: i put: i].<br />
time1 := Time millisecondsToRun:<br />
[<strong>an</strong>Array inject: 0 into: [:sum :item | sum + item ]].<br />
Tr<strong>an</strong>script cr; show: 'Inject '; show: time1 printString.<br />
11.11 Conversion<br />
An inst<strong>an</strong>ce of one collection c<strong>an</strong> be converted into a different sort of collection with certain conditions.<br />
For example, it is possible to convert a Bag inst<strong>an</strong>ce into a Set inst<strong>an</strong>ce, but <strong>an</strong>y duplicate objects in the<br />
Bag inst<strong>an</strong>ce will be removed. For example:<br />
| a b |<br />
a := Bag new.<br />
a add: 'John'.<br />
a add: 'Paul'.<br />
a add: 'John'.<br />
b := a asSet.<br />
It is quite common to w<strong>an</strong>t to take a set or a bag <strong><strong>an</strong>d</strong> to sort it in some m<strong>an</strong>ner. The easiest way of doing<br />
this is to convert it to a sorted collection. For example:<br />
| aBag aSortedCollection |<br />
aBag := Bag new.<br />
aBag add: 'John'; add: 'Paul'; add: 'Denise'; add: 'Fiona'.<br />
aSortedCollection := aBag asSortedCollection.<br />
aSortedCollection do: [:item | Tr<strong>an</strong>script cr; show: item].<br />
This results in the list of names being printed alphabetically in the Tr<strong>an</strong>script (see Figure 11.2).<br />
Figure 11.2: Printing a sorted list in the Tr<strong>an</strong>script<br />
You should note that these conversion messages do no t affect the receiving object in <strong>an</strong>yway. That<br />
is, it does not actually convert the collection, rather it creates a new collection of the correct class <strong><strong>an</strong>d</strong><br />
fills it with the objects in the receiving collection.<br />
The other commonly used conversion messages include:<br />
• asBag. This will return a bag collection<br />
• asSet. This returns a set collection. Note if duplicates existed in the receiving collection, they<br />
will have been ignored.<br />
• asOrderedCollection . This returns <strong>an</strong> OrderedCollection with elements from the<br />
receiver. The ordering is arbitrary.<br />
102