smalltalk: breaking out of a loop..

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

smalltalk: breaking out of a loop..

sergio_101
if i have an OrderedCollection called people..

if i have a million ordered pairs in there, and i wanna see af any one
of them (hasTenBucks = true).

i don't want to iterate all of them, i want to stop when i find that
one hasTenBucks..

so in something like:

people do: [ :person | (person hasTenBucks) ifTrue; [break out of loop] ]

ideas? thanks!

--
----
peace,
sergio
photographer, journalist, visionary

http://www.CodingForHire.com
http://www.coffee-black.com
http://www.painlessfrugality.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101

Reply | Threaded
Open this post in threaded view
|

Re: smalltalk: breaking out of a loop..

Max Leske
use #dectect:


On 07.11.2010, at 17:04, sergio_101 wrote:

> if i have an OrderedCollection called people..
>
> if i have a million ordered pairs in there, and i wanna see af any one
> of them (hasTenBucks = true).
>
> i don't want to iterate all of them, i want to stop when i find that
> one hasTenBucks..
>
> so in something like:
>
> people do: [ :person | (person hasTenBucks) ifTrue; [break out of loop] ]
>
> ideas? thanks!
>
> --
> ----
> peace,
> sergio
> photographer, journalist, visionary
>
> http://www.CodingForHire.com
> http://www.coffee-black.com
> http://www.painlessfrugality.com
> http://www.twitter.com/sergio_101
> http://www.facebook.com/sergio101
>


Reply | Threaded
Open this post in threaded view
|

Re: smalltalk: breaking out of a loop..

sergio_101
On Sun, Nov 7, 2010 at 11:08 AM, Max Leske <[hidden email]> wrote:
> use #dectect:
>

perfect..

thanks!
--
----
peace,
sergio
photographer, journalist, visionary

http://www.CodingForHire.com
http://www.coffee-black.com
http://www.painlessfrugality.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101

Reply | Threaded
Open this post in threaded view
|

Re: smalltalk: breaking out of a loop..

Guillermo Polito
#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

On Sun, Nov 7, 2010 at 1:27 PM, sergio_101 <[hidden email]> wrote:
On Sun, Nov 7, 2010 at 11:08 AM, Max Leske <[hidden email]> wrote:
> use #dectect:
>

perfect..

Reply | Threaded
Open this post in threaded view
|

Re: smalltalk: breaking out of a loop..

Göran Krampe
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!

regards, Göran

Reply | Threaded
Open this post in threaded view
|

Re: smalltalk: breaking out of a loop..

Marcus Denker-4
In reply to this post by Guillermo Polito

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 | Threaded
Open this post in threaded view
|

Re: smalltalk: breaking out of a loop..

Max Leske
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 | Threaded
Open this post in threaded view
|

Re: smalltalk: breaking out of a loop..

Lukas Renggli
In reply to this post by Marcus Denker-4
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 <[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|

Re: smalltalk: breaking out of a loop..

Max Leske
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 <[hidden email]> 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
>