Cool trick! Smalltalk is so cool!! :D

On 08.11.2010, at 17:50, Marcus Denker 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.
> 
> 


Reply via email to