Home Segments Index Top Previous Next

306: Mainline

A more elegant definition of factorial uses a to:do: message.

The first argument of a to:do: message, with the receiver, determines an inclusive integer range.

The second argument is a block that contains a block parameter. The block parameter is identified by a prefixing colon, and the parameter is separated from the statements in the block by a bar:

[:parameter name | statements] 

When an Integer instance receives a to:do: message, that integer evaluates the do: argument, a block, once for each integer in the inclusive integer range. Each evaluation is done with one of the integers in the range assigned to the parameter.

For example, to display all the numbers from 1 to 4 in the Transcript, you deploy to:do: as follows:

Workspace
1 to: 4 do: 
  [:anInteger | Transcript show: anInteger printString; space] 
Transcript 
1 2 3 4