s LearnSqueak.com

ABSOLUTE BEGINNER

Your first isEmpty: ask whether anything is there

Ask a word or list whether it contains anything, then watch a collection change in five tiny visual Squeak steps.

5 minutes · 5 small steps · Squeak 6.1

STEP 1

Ask an empty piece of text

Open a Workspace from the World menu: open..., workspace. Type two single quotes with nothing between them. Select the whole expression and choose Print it. isEmpty asks whether the text contains zero characters.

Select the whole expression, then Print it
'' isEmpty
NATIVE SQUEAK CAPTURE
Native Squeak empty collection checkpoint 1
Actual native Squeak Workspace Print it result.
You should see

true. The empty text is a real object, and it has no characters.

STEP 2

Put something between the quotes

Now the text holds two characters. The same question can have a different answer because the receiver is different.

Select the whole expression, then Print it
'Hi' isEmpty
NATIVE SQUEAK CAPTURE
Native Squeak empty collection checkpoint 2
Actual native Squeak Workspace Print it result.
You should see

false. Try a single space between the quotes. That also answers false, because a space is one character.

STEP 3

Ask an empty list

The notation #() makes an empty Array. You can send isEmpty to this collection too. You do not have to count its entries yourself.

Select the whole expression, then Print it
#() isEmpty
NATIVE SQUEAK CAPTURE
Native Squeak empty collection checkpoint 3
Actual native Squeak Workspace Print it result.
You should see

true. The list contains no entries. An empty list and empty text are different kinds of object, but both understand this message.

STEP 4

Keep zero separate from empty

This list contains one number. That number happens to be zero. The value of an entry is different from the number of entries.

Select the whole expression, then Print it
#(0) isEmpty
NATIVE SQUEAK CAPTURE
Native Squeak empty collection checkpoint 4
Actual native Squeak Workspace Print it result.
You should see

false. There is one entry. Try #(nil): it also answers false, because that list contains one entry too.

STEP 5

Watch a list become empty again

Copy all the lines, including the bars. OrderedCollection is a list whose size can change. We ask before adding a number, after adding it, and after removing it. The final braces collect the three answers.

Select the whole expression, then Print it
| basket before during after |
basket := OrderedCollection new.
before := basket isEmpty.
basket add: 7.
during := basket isEmpty.
basket remove: 7.
after := basket isEmpty.
{before. during. after}
NATIVE SQUEAK CAPTURE
Native Squeak empty collection checkpoint 5
Actual native Squeak Workspace Print it result.
You should see

#(true false true). Empty, then occupied, then empty again. isEmpty reports the current contents of the collection.

YOU DID IT

A small step. A real result.

isEmpty asks whether a collection has zero entries. A zero, a space, or nil inside a collection still counts as an entry.

Optional code download: open it as text and copy one example into Workspace.

Download the five examples; copy one at a time

Your first list · More Smalltalk messages