help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-smalltalk] Re: How to exit loops?


From: nicolas cellier
Subject: [Help-smalltalk] Re: How to exit loops?
Date: Sun, 22 Jun 2008 19:21:05 +0200
User-agent: Thunderbird 2.0.0.12 (X11/20080306)

Lukas Renggli a écrit :
My preferred way is the following:

Add the following method to Object (or somewhere else):

Object>>escaper: aBlock
   ^ aBlock value: [ ^ nil ]

Then implement your loop like this:

self escaper: [ :break |
   1 to: 10 do: [ :x |
      x = 3 ifTrue: [ break value ].
      ... ] ]

Cheers,
Lukas


Very Smaltalk-ish!

Even works if you want to escape nested loop:

self escaper: [:breakOuter |
   1 to: 10 do: [:i |
      self escaper: [:breakInner |
         1 to: 10 do: [:j |
            j = 3 ifTrue: [breakInner value].
            (j = 2 and: [i = 7]) ifTrue: [breakOuter value]]]]].

Nicolas





reply via email to

[Prev in Thread] Current Thread [Next in Thread]