Help - Programming - Collect and List

Previous - Programming - Next

Everyone collects things and makes lists. In Squeak this can be done very easily and in many ways. In this section I give a very simple approach and that requires that you start with filing in a little code. You can find it by opening a FileList on /Help/Programming, finding the file that's called Lister.st and instructing the FileList to fileIn.

1. Using the code

It's not much code, so filing in and installing will happen very rapidly, and you can see the result in a SystemBrowser: There will now be a category Help in which there will be a class Lister. Go to the class, read the ClassComment (under '?') and try the example in the ClassComment, to see what Lister can produce.

Here it is for your convenience

Lister new openWithTitle: 'listing it....'.

Incidentally, I use a convention in my comments to the code of Lister that I found useful but uncommon: In comments to code I enclose the terms that name instanceVariables or classVariables between <> so that I instantly know that these are dataslots. This is a way to avoid writing out "instanceVariables" etc. and to know immediately where data are kept. (ClassVariables start with uppercase; instanceVariables with lowercase.)

You can find all instanceVariables and classVariables by way of the buttons inst vars and class vars in a SystemBrowser. If you click on any of them in the pop-up menu, Squeak will produce a window with a list of all places in the code of the object where the inst var or class var is used.

So we have a means of making lists, that I shall say a little more about below. Here I merely note that there is a menu by way of the right mouse button that allows you to insert, delete and edit items in a Lister, and to inspect the Lister itself.

First there is this question: How do we get or collect stuff to list, apart from inserting it as we can in Lister already?

Well, there are many ways, but one of the simplest in Squeak is simply to use the wordprocessing capacities that Squeak has, and produce lists from a Workspace or indeed from Scamper. Select and do the following lines:

| text myData myList |

text := 'Everyone collects things and makes lists. In Squeak this can be done very easily and in many ways. In this section I give a very simple approach and that requires that you start with filing in a little code. You can find it by opening a FileList on /Help/Programming, finding the file that is called Lister.cs and instructing the FileList to fileIn.

It is not much code, so it will happen very rapidly, and you can see the result in a SystemBrowser: There will now be a category Help in which there will be a class Lister. Go to the class, read the ClassComment and try the two examples in the ClassComment, to see what Lister can produce.'.

myData := text findTokens: '.'.

myList := Lister new.
myList initList: myData at: 1.
myList openWithTitle: '** This fancy list **'.

Here the text is the beginning of this section (beteen quotes, assigned to a variable named text). In the next line, this text is used with findTokens: which is a String method that's very useful to split up the text at the places of dots in the text. This produces an OrderedCollection of strings. Next, myList is a new Lister with myList containing the data in myList, that's told to open with a title. (If you expand the window of myList it may be easier to read all of the text.)

Now this suggests a very easy very general way to make and display your own lists of things when in Squeak: Use the approach we just used, but with some convenient separator to be used in findTokens: . Here is an example

| text myData myList |

text := ' Boole, Peirce, Cantor, Russell, Zermelo, Bernays, Turing, Church, Curry, Kripke, Montague.'.
myData := text findTokens: ','.

myList := Lister new.
myList initList: myData at: 1.
myList openWithTitle: 'Some logicians'.

One of the features in a PluggableListMorph is that it can find entries from the keyboard: Repeatedly type 'c' in this list to find logicians whose name started with a 'c'.

Having findTokens: you can split the text for an list differently by introducing another separator, e.g. with the following that you can paste into the above and do - though any string can serve as separator:

text := ' Boole, Peirce, Cantor, Russell, Zermelo, Bernays, Turing, Church, Curry, Kripke, Montague.'.
myData := text findTokens: ''.

It is also easy to get other types of lists. Here is one that is based on calculations:

| count choice myData myList |

myData := OrderedCollection new.
count := 0.
[ count < 101 ] whileTrue:
[choice := 100 take: count. myData add: choice. count := count+5].

myList := Lister new.
myList initList: myData at: 1.
myList openWithTitle: 'Some choices'.

Now at this point you might ask: OK, I can gather and display data (i.e. collect and list them) - but then what can I do? The answer is: Everything Squeak can do with the data - for these are in Lister in the shape of an OrderedCollection in any case, and this OrderedColection is a directly accessible instanceVariable called list in Lister.

Here is a way to show this and get to these data: Do the above piece of code, and click up the halos twice. The first gives you the SystemWindow the Lister is in; the second gives you the Lister in the SystemWindow, which was derived from a PluggableListMorph and hence has that class name. Having the halos for PluggableListMorph click the halo with the wrench and from the menu you then get click inspect morph.

Et voil - an inspector on your list. If you go to the lower code pane, and copy, paste and do the following line

list at: 5

you get the fifth item of list (which is the instance variable of Lister that keeps the OrderedCollection of data of Lister).

So... you wanted to do something with the data in a list? Here is a some code you can copy and paste into the lower code pane of the inspector of the list and then do by selecting and Cmd-d:

| count end choice myData myList mySin |

myData := list.
end := myData size + 1.
count := 1.
[ count < end ] whileTrue:
[choice := myData at: count. mySin := choice sin. myData add: mySin. count := count+1].
list := myData.
myList := Lister new.
myList initList: myData at: 1.
myList openWithTitle: 'Data plus sins'.

This produces a list from the original data to which the sinuses of the entries are added. (Note the numbers used here are so large or small that Squeak at several places doesn't calculate them.)

2. Some context of the code

Having seen Lister run, let's consider some context.

First, there is this general point:

The best and the fastest way to learn about Squeak's code is by using the following facilities:

Cmd-p = Print It = Do It and print the result behind it. 5+4+3+2+1
Cmd-b = Browse It = start a browser on the object with the highlighted name. Project
Cmd-i = Inspect It = Do It and start an inspector on the result. #('One' 'Two' 'Three' 'Go') asSet.
Cmd-m = Make a collection of implementors of the term. collect:
Cmd-n = Make a collection of senders of the term. do:

Cmd-k = Start a new workspace (when the cursor is in the Project window, not on some morph inside it)
Cmd-t = Start a transcript (when the cursor is in the Project window, not on some morph inside it)

Cmd-. = Stop it = Cmd key followed by a dot ends whatever activity Squeak is busy with and returns to the state that was before that activity started. (Use this when you think something is wrong or takes too long.)

There are many more command-keys, notably in a Workspace. Here is a list of those that work in a workspace:

Utilities openCommandKeyHelp. "Select and Cmd-d pops them up. Also in Worldmenu-help."

Of these especially the first five I listed are extra-ordinarily helpful, together with the fact that a Workspace maintains its own variables, which one therefore doesn't need to declare in it.

Now... let's first take a look at Lister. (Highlight and Cmd-b.)

If you look at it in the Browser, you see that it is not much code, and most of the code relates to setting and getting the two instance variables of Lister, which are called list and selectedIndex, and which are used, respectively, for keeping an OrderedCollection to be listed, and the number of the OrderedCollection in the list that should be highlighted.

Indeed, most of the work Lister can do it does by way of its methods listMenu: and openWithTitle:. (Incidentally: highlighting these methods and using Cmd-m also produces a Browser with these methods in Lister. It is very easy to have Squeak search for code in Squeak and this is very convenient when writing code or learning about it!)

Now part of the code we used above involved findTokens: . If you highlight this and do Cmd-m you'll see it in its context in the String class of which it is a method. It (and some other methods in String) are very useful for taking apart and manipulating strings.

Again, another part of Lister used an OrderedCollection. If you do a Cmd-b on it, you get the code for this very useful class, that helps you to store, search and maintain whatever can be put into an OrderedCollection in Squeak.

Finally, in this short roundtrip looking for context of Lister we can look at PluggableListMorph, which Lister uses. You'll find that it has far more possibilies as ready made methods than are used by Lister, which is just a very simple use of PluggableListMorph.

It will now also be obvious in principle that Browsers indeed also use PluggableListMorph though usually with some more facilities than we provided in Lister. And it should be obvious in principle how you can work with your own lists.

Since we are looking at the context of code:

Remember that much of what you want to know about and find in Squeak's code can be found by way of the buttons of browsers. Here are some brief comments, and you are invited to try these out with the code in and related to Lister

browse: browses the class
senders: browses the senders
implementors: browsers the implementors
versions: shows the earlier versions of the code
inheritance: shows inheritors
hierarchy: opens a hierarchy browser
inst vars: displays the instance variables and links to the places used in the pop-up
class vars: displays the class variables and links to the places used in the pop-up
source: various ways of displaying the source

In the last option source, especially the options documentation - source - colorprint are very useful, while altSyntax shows an alternative way of writing Squeak code, and byteCodes displays a string-version of the code as compiled by Squeak when it accepted the code.

Previous - Programming - Next