Loops
Damien Cassou, Stéphane Ducasse and Luc Fabresse
http://stephane.ducasse.free.fr
Loops
- Loops are expressed as messages
- Many different ones
- Plain loops
- Conditional loops
- Messages sent to numbers, collections or blocks
- Iterators
Loops: timesRepeat:
To repeat a given number of times an action
Loops: to:do:
to:do:
is a method defined on Number
Example: to:do:
The block is executed with the temporary i
taking values from 1 to 100
Loops: to:by:do:
to:by:do:
is also a method defined on Number
Example: to:by:do:
The block is executed with i taking values from 1 to 100 by step of 3
Basic Iterators Overview
do:
(iterate)
collect:
(iterate and collect results)
select:
(select matching elements)
reject:
(reject matching elements)
detect:
(get first element matching)
detect:ifNone:
(get first element matching or a default value)
includes:
(test inclusion)
- and a lot more...
Loops: do:
The block is executed with each
taking as value all the elements of aCol
Example: The iterator do:
Loops: whileTrue:
Executes the argument, aBlock
, as long as the value of the receiver is true
Loops: whileTrue
Executes the receiver, as long as its value is true
Equivalent with whileFalse
and whileFalse:
Summary
- Loops are expressed as messages
- Many different ones
- Plain loops
- Conditional loops
- Messages sent to numbers, collections or blocks
- Iterators
/