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.
This is a very powerful class which greatly simplifies the production of ordered lists of information.<br />
You c<strong>an</strong> ch<strong>an</strong>ge the block controlling the sort by sending the message sortBlock: to the<br />
sortedCollection inst<strong>an</strong>ce with a new block definition.<br />
11.3 List<br />
The List class is a recent addition to the collection classes in VisualWorks (which is not yet available<br />
in other implementations of <strong>Smalltalk</strong> such as Visual <strong>Smalltalk</strong>). The List class combines some of the<br />
more useful features of the Array, OrderedCollection, SortedCollection classes <strong><strong>an</strong>d</strong> the<br />
Model class. The order of its elements is initially determined by the order in which the elements are<br />
added (as in OrderedCollection). These elements c<strong>an</strong> then be accessed via <strong>an</strong> index (as in<br />
Array). However, the elements c<strong>an</strong> also be sorted using either the default ascending order, or a custom<br />
sort block c<strong>an</strong> be provided (as in SortedCollection). In addition a List keeps a record of<br />
dependents to which it should send updates (as in class Model). However, as we have not yet looked at<br />
the Model class, we will ignore this part of the class’s operation.<br />
The set of features provided by this class make it one of the most widely used types of collection, in<br />
some cases suppl<strong>an</strong>ting the older OrderedCollection <strong><strong>an</strong>d</strong> SortedCollection classes. It responds to a<br />
combination of the Ordered <strong><strong>an</strong>d</strong> Sorted Collection class protocols. Therefore it underst<strong><strong>an</strong>d</strong>s after:,<br />
before:, sort, sortWith:, add:, add:after: <strong><strong>an</strong>d</strong> add:before:. It also responds to<br />
addLast:, addFirst:, remove:ifAbsent:, removeFirst, removeLast etc.<br />
To create a new inst<strong>an</strong>ce of class List use either new or new: depending on whether you wish to<br />
specify <strong>an</strong> initial size or not.<br />
For example, try this out in a Workspace.<br />
| temp |<br />
temp := List new: 10.<br />
temp add: 'John'.<br />
temp addFirst:'Paul'.<br />
temp add: 'Peter' before: 'John'.<br />
temp inspect.<br />
11.4 Intervals<br />
Inst<strong>an</strong>ces of class Interval represent finite arithmetic progressions. Such progressions are given by a<br />
starting number, a (finite) limit, <strong><strong>an</strong>d</strong> a method of computing the next number. Once created, new<br />
elements c<strong>an</strong>not be added or removed from the Interval.<br />
Intervals c<strong>an</strong> be created using the class messages:<br />
from: start to: stop<br />
from: start to: stop by: step<br />
start, stop <strong><strong>an</strong>d</strong> step c<strong>an</strong> be <strong>an</strong>y kind of Number. Intervals are common enough that a shorth<strong><strong>an</strong>d</strong><br />
form has been provided. This shorth<strong><strong>an</strong>d</strong> form is achieved by sending to: or to:by: to a kind of<br />
Number. For example the following are equivalent:<br />
Interval from: 10 to: 100 by: 2<br />
10 to: 100 by: 2<br />
These are equivalent to a for i = 10 to 100 step 2 type loop. However, they actually create<br />
<strong>an</strong> interval containing all the obje cts to be processed before <strong>an</strong>y further processing occurs. This<br />
allows them to be used with a do loop.<br />
Intervals respond to the message do: aBlock by evaluating aBlock for each of its values<br />
in sequence. Thus, <strong>an</strong> Interval c<strong>an</strong> be used to construct the equivalent of a FOR loop. For example:<br />
(10 to: 100 by: 2) do: [:each | Tr<strong>an</strong>script show: each printString].<br />
This is therefore roughly equivalent to the C for loop:<br />
96