[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Help-smalltalk] Re: How to exit loops?
From: |
Canol Gokel |
Subject: |
[Help-smalltalk] Re: How to exit loops? |
Date: |
Sun, 22 Jun 2008 20:23:15 +0000 (UTC) |
User-agent: |
Loom/3.14 (http://gmane.org/) |
nicolas cellier <ncellier <at> ifrance.com> writes:
>
> 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
>
Thank you for your answers, I also found a solution by extending the Number
class like this:
Number extend [
to: stop do: aBlock butPleaseExitIf: aCondition [
<category: 'shortcuts and iterators'>
| i |
i := self.
[(i <= stop) and: [(aCondition value: i) not]] whileTrue:
[aBlock value: i.
i := i + self unity]
]
]
This to:do:butPleaseExitIf: selector allows you to use expressions like:
1 to: 10 do: [:x | x printNl.] butPleaseExitIf: [:x | x = 3.]