s LearnSqueak.com

ABSOLUTE BEGINNER

Your First anySatisfy: in Squeak

Ask whether at least one item in a list passes a test.

5 minutes · 5 small steps · Squeak 6.1

STEP 1

Ask one number

Start with one yes-or-no question: is seven greater than five?

Select the whole expression, then Print it
7 > 5
NATIVE SQUEAK CAPTURE
Native Squeak Workspace: Ask one number gives true
Native Squeak Workspace showing the code and its actual evaluated result.
You should see

Change 7 to 2 and print false.

STEP 2

Ask about a list

The list tries the rule on its items. Only one item needs to pass.

Select the whole expression, then Print it
#(1 7 3) anySatisfy: [:n | n > 5]
NATIVE SQUEAK CAPTURE
Native Squeak Workspace: Ask about a list gives true
Native Squeak Workspace showing the code and its actual evaluated result.
You should see

Try 6 in place of 7. It still prints true.

STEP 3

Try a list with no match

None of the numbers is greater than five, so the answer is false.

Select the whole expression, then Print it
#(1 2 3) anySatisfy: [:n | n > 5]
NATIVE SQUEAK CAPTURE
Native Squeak Workspace: Try a list with no match gives false
Native Squeak Workspace showing the code and its actual evaluated result.
You should see

Change 3 to 8; the answer changes to true.

STEP 4

Try the empty list

No item can pass when the list is empty.

Select the whole expression, then Print it
#() anySatisfy: [:n | n > 5]
NATIVE SQUEAK CAPTURE
Native Squeak Workspace: Try the empty list gives false
Native Squeak Workspace showing the code and its actual evaluated result.
You should see

Add just 9 to the empty list and try again.

STEP 5

Ask about words

The rule can ask about words too. Rabbit has more than four letters, so the result is true.

Select the whole expression, then Print it
#('cat' 'rabbit' 'ox') anySatisfy: [:word | word size > 4]
NATIVE SQUEAK CAPTURE
Native Squeak Workspace: Ask about words gives true
Native Squeak Workspace showing the code and its actual evaluated result.
You should see

Remove 'rabbit' and print it again.

YOU DID IT

A small step. A real result.

anySatisfy: answers true if at least one item passes your test. Otherwise it answers false.

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

Download the five examples; copy one at a time

Count matching items · Your first list