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
"Mark" == Mark Volkmann mark@ociweb.com 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 <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=br...
"Nicolas" == Nicolas Cellier ncellier@ifrance.com writes:
Nicolas> For the fun of it, see also
Nicolas> http://article.gmane.org/gmane.comp.lang.smalltalk.gnu.general/3375/match=br...
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. :)
Am 03.10.2008 um 09:27 schrieb Randal L. Schwartz:
"Nicolas" == Nicolas Cellier ncellier@ifrance.com writes:
Nicolas> For the fun of it, see also
Nicolas> http://article.gmane.org/gmane.comp.lang.smalltalk.gnu.general/3375/match=br...
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 -
"Bert" == Bert Freudenberg bert@freudenbergs.de writes:
Bert> That would be a perfect opportunity to employ #valueWithPossibleArgument:.
And with this, the newbies heads have exploded. :)
Am 03.10.2008 um 11:02 schrieb Randal L. Schwartz:
"Bert" == Bert Freudenberg bert@freudenbergs.de 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 -
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 bert@freudenbergs.dewrote:
Am 03.10.2008 um 11:02 schrieb Randal L. Schwartz:
"Bert" == Bert Freudenberg bert@freudenbergs.de 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 Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
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@lists.squeakfoundation.org