Control flow of looping

Alan Kay Alan.Kay at squeakland.org
Fri Apr 12 15:50:13 UTC 2002


Tim --

Your second suggestion is "very Smalltalk" like. It's interesting 
that Smalltalk-72 did have both "again" and "leave" for its loops, 
but that these weren't considered necessary by the time Smalltalk-80 
came around -- in part because it was felt that "mapping" loops a la 
LISP and APL were much nicer (I tend to agree).

Cheers,

Alan

At 9:08 AM -0500 4/13/02, Tim Olson wrote:
>  >Kamil Kukura <kamk at volny.cz> writes:
>>
>>>  Is it possible in loops such as "true-or-false whileTrue: [ block ]" do
>>>  the iteration repeat or break out similiar to 'break' and 'continue'
>>>  keywords in C?
>
>Martin Drautzburg wrote some good advice about breaking methods up into
>smaller ones, and looking at the collection iteration methods
>(collect/select/detect).
>
>But since Smalltalk control flow constructs are written in Smalltalk, it
>is definitely possible to create a breakable/continuable while loop
>control structure.
>
>The following examples aren't recommended for actual use, just as
>examples of how you can extend Squeak's control flow constructs directly
>in Squeak.
>
>One way is to create return and continue blocks which are passed in to
>the loop as arguments.
>For example, adding the following two methods to the BlockContext class:
>
>----
>
>breakableWhileTrue: aBlock
>	| returnBlock |
>	returnBlock := [:result | ^ result].
>	self whileTrue: [self continuableWhileTrue: aBlock returnBlock:
>returnBlock]
>
>
>continuableWhileTrue: aBlock returnBlock: returnBlock
>	| continueBlock |
>	continueBlock := [^ self].
>	^ aBlock value: returnBlock value: continueBlock
>
>----
>
>allows you to write loops like:
>
>n := 0.
>m := 0.
>[n := n + 1. n < 50] breakableWhileTrue: [:return :continue |
>	n > 10 ifTrue: [return value: m].
>	n odd ifTrue: [continue value].
>	m := m + 1].
>
>
>Another way is to piggyback on the powerful exception handling
>capabilities exisiting in Squeak.  You can define two new Exception
>subclasses: LoopExit and LoopContinue.  Then add a new loop method to
>BlockContext:
>
>breakableWhileTrue: aBlock
>self whileTrue:
>	[[[aBlock value: LoopExit value: LoopContinue]
>		on: LoopExit do: [:ex | ^ ex messageText]]
>			on: LoopContinue do: []]
>
>Then your loop would look like:
>
>n := 0.
>m := 0.
>[n := n + 1. n < 50] breakableWhileTrue: [:return :continue |
>	n > 10 ifTrue: [return signal: m].
>	n odd ifTrue: [continue signal].
>	m := m + 1].
>
>
>
>      -- tim


-- 



More information about the Squeak-dev mailing list