[Pharo-project] smalltalk: breaking out of a loop..

Max Leske maxleske at gmail.com
Sat Nov 13 16:00:40 CET 2010


Actually, I've read that already. I had just forgotten :)
Thanks.

Be prepared for a coooollld switzerland :-p

Chees.

On 13.11.2010, at 15:55, Lukas Renggli wrote:

> For that matter also see this blog post and the discussion:
> <http://www.lukas-renggli.ch/blog/continue-break>.
> 
> Lukas on Mate
> 
> On Monday, November 8, 2010, Marcus Denker <marcus.denker at inria.fr> wrote:
>> 
>> On Nov 7, 2010, at 5:50 PM, Göran Krampe wrote:
>> 
>>> On 11/07/2010 08:06 PM, Guillermo Polito wrote:
>>>> #anySatisfy: does not iterate over the entire collection either.  So if
>>>> you only want to know if there is one that satisfies the condition...
>>>> this is the message you're looking for.
>>>> 
>>>> Cheers!
>>>> Guille
>>> 
>>> And finally, a generic "trick" for breaking out of loops (not just iterations over collections):
>>> 
>>> Factor out the loop in a single method, then call it. When you want to break out, just do a normal return!
>> 
>> This often even makes the code more readable.
>> 
>> But speaking of tricks...
>> 
>> testValueWithExitBreak
>> 
>>        | val |
>> 
>>        [ :break |
>>            1 to: 10 do: [ :i |
>>                        val := i.
>>                        i = 4 ifTrue: [break value].
>>                ]
>>        ] valueWithExit.
>> 
>>        self assert: val = 4.
>> 
>> 
>> 
>> testValueWithExitContinue
>> 
>>        | val last |
>>        val := 0.
>> 
>>        1 to: 10 do: [ :i |
>>                [ :continue |
>>                        i = 4 ifTrue: [continue value].
>>                        val := val + 1.
>>                        last := i
>>                ] valueWithExit.
>>        ].
>> 
>>        self assert: val = 9.
>>        self assert: last = 10.
>> 
>> 
>> with
>> 
>> BlockClosure>>valueWithExit
>>          self value: [ ^nil ]
>> 
>> 
>> 
>> 
>> 
>> 
>> --
>> Marcus Denker  -- http://www.marcusdenker.de
>> INRIA Lille -- Nord Europe. Team RMoD.
>> 
>> 
>> 
> 
> -- 
> Lukas Renggli
> www.lukas-renggli.ch
> 





More information about the Pharo-project mailing list