break out of do

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

break out of do

Mark Volkmann
Is there a way to break out of a do:? For example, if I use do: to  
iterate through the characters in a string and I find a character I  
don't want to allow, can I break out and avoid examining the remaining  
characters?

---
Mark Volkmann




_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: break out of do

Randal L. Schwartz
>>>>> "Mark" == Mark Volkmann <[hidden email]> writes:

Mark> Is there a way to break out of a do:? For example, if I use do: to
Mark> iterate through the characters in a string and I find a character I
Mark> don't want to allow, can I break out and avoid examining the remaining
Mark> characters?

The simplest way is to factor your code such that when you are done processing
in your loop, you answer from the method when you've reached that.

    contains: anItem

    self do: [:each|
      "some stuff here if you want"
      anItem = each ifTrue: [^true]. "breaks the loop, returns true to caller"
      "some more stuff here if you want"
    ].
    ^false "default answer if not found"
 
I'd suggest trying to get your code shaped like that.  It'll be the
most familar.

There are more complicated ways to do it with exceptions if you need
to stay within the same method.  Don't do that. :)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[hidden email]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: break out of do

K. K. Subramaniam
In reply to this post by Mark Volkmann
On Friday 03 Oct 2008 6:34:24 pm Mark Volkmann wrote:
> Is there a way to break out of a do:? For example, if I use do: to
> iterate through the characters in a string and I find a character I
> don't want to allow, can I break out and avoid examining the remaining
> characters?
A string is a collection and there are a rich set of methods for iterating
over a collection. Bring up method search tool (ALT+Shift+W or
WorldMenu->windows->find message names) and enter "includes" you will find
methods like includes:, includesAnyOf:, includesAllOf:, anySatisfy: and so
on.

The Method search tool is a really nifty tool for searching out methods.

Subbu
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: break out of do

Nicolas Cellier-3
In reply to this post by Randal L. Schwartz
Randal L. Schwartz <merlyn <at> stonehenge.com> writes:

>
> snip...
>
> There are more complicated ways to do it with exceptions if you need
> to stay within the same method.  Don't do that. :)
>

For the fun of it, see also

http://article.gmane.org/gmane.comp.lang.smalltalk.gnu.general/3375/match=break



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Re: break out of do

Randal L. Schwartz
>>>>> "Nicolas" == Nicolas Cellier <[hidden email]> writes:

Nicolas> For the fun of it, see also

Nicolas> http://article.gmane.org/gmane.comp.lang.smalltalk.gnu.general/3375/match=break

Oww.  I remember that.  It makes my head hurt.

You could even go further:

    Object >> valuedEscaper: aBlock
       ^ aBlock value: [ :finalValue | ^finalValue ]

    ...

    found := self valuedEscaper: [:escape |
           1 to: 10 do: [ :x |
               x = 3 ifTrue: [escape value: true].
           ]
    false].

Oh geez.  I think I just reinvented continuations. :)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[hidden email]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Re: break out of do

Bert Freudenberg

Am 03.10.2008 um 09:27 schrieb Randal L. Schwartz:

>>>>>> "Nicolas" == Nicolas Cellier <[hidden email]> writes:
>
> Nicolas> For the fun of it, see also
>
> Nicolas> http://article.gmane.org/gmane.comp.lang.smalltalk.gnu.general/3375/match=break
>
> Oww.  I remember that.  It makes my head hurt.
>
> You could even go further:
>
>    Object >> valuedEscaper: aBlock
>       ^ aBlock value: [ :finalValue | ^finalValue ]
>
>    ...
>
>    found := self valuedEscaper: [:escape |
>           1 to: 10 do: [ :x |
>               x = 3 ifTrue: [escape value: true].
>           ]
>    false].
>
> Oh geez.  I think I just reinvented continuations. :)



That would be a perfect opportunity to employ  
#valueWithPossibleArgument:.

- Bert -


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Re: break out of do

Randal L. Schwartz
>>>>> "Bert" == Bert Freudenberg <[hidden email]> writes:

Bert> That would be a perfect opportunity to employ  #valueWithPossibleArgument:.

And with this, the newbies heads have exploded. :)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[hidden email]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Re: break out of do

Bert Freudenberg

Am 03.10.2008 um 11:02 schrieb Randal L. Schwartz:

>>>>>> "Bert" == Bert Freudenberg <[hidden email]> writes:
>
> Bert> That would be a perfect opportunity to employ  
> #valueWithPossibleArgument:.
>
> And with this, the newbies heads have exploded. :)

Hehe :) If they followed the thread closely they will have noticed  
that this is a tangent, the original recommendation was to factor this  
out into a separate method and use a local return. And that's still  
the way you should do it.

Something to take away would be that Squeak lets you implement the  
control structures you need rather easily, which is great once you  
need it.

- Bert -


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Re: break out of do

Rob Rothwell
If my head didn't explode, does that mean I'm not quite as new as I still feel?

I still have to keep smacking myself to identify the real objects and their behavior instead of pushing things around, but I actually followed this!

Thanks...it was a good example that made blocks seem a little "friendlier."

Take care,

Rob

On Fri, Oct 3, 2008 at 2:14 PM, Bert Freudenberg <[hidden email]> wrote:

Am 03.10.2008 um 11:02 schrieb Randal L. Schwartz:


"Bert" == Bert Freudenberg <[hidden email]> writes:

Bert> That would be a perfect opportunity to employ  #valueWithPossibleArgument:.

And with this, the newbies heads have exploded. :)

Hehe :) If they followed the thread closely they will have noticed that this is a tangent, the original recommendation was to factor this out into a separate method and use a local return. And that's still the way you should do it.

Something to take away would be that Squeak lets you implement the control structures you need rather easily, which is great once you need it.

- Bert -



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners