ABSOLUTE BEGINNER
Your First Match Count in Squeak
Use count: to find how many items in a list pass a simple test.
STEP 1
Ask one yes-or-no question
The > sign asks whether the number on the left is bigger than the number on the right.
7 > 5
7 is bigger than 5, so Squeak prints true.
STEP 2
Count the numbers that pass
Read the part in brackets as: for each number n, ask if n is bigger than 5. The numbers 7 and 9 pass. The number 5 does not.
#(2 5 7 9) count: [:n | n > 5]
Squeak prints 2. You get the total, not the matching numbers.
STEP 3
Change just the test
Keep the same list. Change > to >= to include numbers equal to 5 as well as numbers bigger than 5.
#(2 5 7 9) count: [:n | n >= 5]
Squeak prints 3 because 5, 7, and 9 now pass.
STEP 4
Count both copies
The two 7s sit in different places in the list. Each one counts.
#(7 2 7) count: [:n | n > 5]
Squeak prints 2, even though both matches are the same number.
STEP 5
Try a list with no matches
Neither number is bigger than 5. Try replacing the list with #() to see what happens with an empty list.
#(2 5) count: [:n | n > 5]
Both the list shown and the empty list give 0.
YOU DID IT
A small step. A real result.
count: asks a yes-or-no question about each item and answers with the number of yeses.
Optional code download: open it as text and copy one example into Workspace.