[squeak-dev] Escaping from loops in scripts
nicolas.cellier.aka.nice at gmail.com
Sun Dec 9 10:37:21 UTC 2018
when writing scripts, I often need the ability to interrupt a loop.
This is the traditional break instruction of C/Python or more advanced exit
of FORTRAN/ADA (we can tell from which nexted loop we exit).
I could use exceptions, but then handling exit from nested loops is tricky
One possible way to do it in Smalltalk is to use blocks, and interrupt with
non local return.
"Give the ability to exit execution of aBlock by returning control to
aBlock will take an argument, which is a door for returning control.
This is useful for breaking a loop, for example:
self escape: [:exit | someCollection do: [:e | e fullfilSomeCondition
ifTrue: [exit value]. e doSometing]]"
aBlock value: [^self]
| sum |
sum := 0.
self escape: [:exitOuterLoop | someCollection do: [:e |
self escape: [:exitInnerLoop | someOtherCollection do: [:e2 |
e2 > e ifTrue: [exitOuterLoop value].
e2 = e ifTrue: [exitInnerLoop value].
sum := sum + e]]]].
We can also use the escape: inside the loop to avoid chained ifTrue:ifFalse:
but it's then convenient to let escape: return a value:
^aBlock value: [:result | ^result]
aCollection collect: [:e |
e escape: [:return |
e < 0 ifTrue: [return value: e].
e > 20 ifTrue: [return value: 401 ln].
(e squared + 1) ln]]
At that time, I found that amusing, now I find it useful.
I don't see such support in trunk image, could we add it?
Do you think of a better name?
(not setjmp: please)
Unless you come with better alternatives... One thing that is questionable
is that the receiver of escape: is void (escape: is a utility).
It start looking like FORTRAN/ADA exit instruction with explicit naming of
loop but I don't like it better.
Maybe sending the message to the block itself sounds less arbitrary.
^self value: [:result | ^result]
| sum |
sum := 0.
[:exitOuterLoop | someCollection do: [:e |
[:exitInnerLoop | someOtherCollection do: [:e2 |
e2 > e ifTrue: [exitOuterLoop value: nil].
e2 = e ifTrue: [exitInnerLoop value: nil].
sum := sum + e]] handleExit ]] handleExit.
Though, I don't find such post-fixing satisfactory: exitOuterLoop and
handleExit are too far from each other... Especially in scripts that tend
to be longer than ordinary methods (we don't want to factor every quick and
dirty procedure into proper classes/methods when there is no reuse
objective or when they are too specific).
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Squeak-dev