Hello everyone,
Short question: Does Smalltalk have a way to break out of a block like "break" does in C?
Long question: I'm reading in/parsing some not-always-nicely formed data files (as a part of my undergrad research at the NRRI [http://www.nrri.umn.edu/cwe/]). Below is a block of code to handle some of these little uglys that pop up now and then in the datasets.
"ditch anything we don't need, and put in things like elevation and pipeup into params" contents do: [ :coll | 1 to: (coll size) do: [ :i | ((coll at: i) beginsWith: 'ice:') ifTrue: [ coll at: i put: (((coll at: i) findTokens: 'ice:') first) ]. ((coll at: i) beginsWith: ':ELEVATION:') ifTrue: [ coll removeAt: i. params at: #elevation put: (((coll at: i) findTokens: ':ELEVATION:') first) ]. ((coll at: i) beginsWith: ':PIPEUP:') ifTrue: [ coll removeAt: i. params at: #pipeup put: (((coll at: i) findTokens: ':PIPEUP:') first) ]]].
(contents is a Dictionary filled with strings associated with OrderedCollections)
Not the nicest looking code, I know. :P Let's say that a value with ":ELEVATION:" is found, and the element at i is removed. Of course, it runs into a problem accessing this when it looks for ":PIPEUP:". In C, in ":ELEVATION:"'s ifTrue block, we'd just say break; to skip the rest of the stuff in the block. I know I could add some silly variable and another ifTrue, but I am (always) looking for the better way to do things...
regards and thanks in advance! Aaron
On Tuesday 29 May 2001 15:25, reic0024@d.umn.edu wrote:
Hello everyone,
Short question: Does Smalltalk have a way to break out of a block like "break" does in C?
yes and no. What (many/most) Smalltalks have is the ability of returning from the inside of a block:
innerMethod1 true whileTrue: [ done ifTrue: [ ^self ] ]
outerMethod true whileTrue: [ self innerMethod1. self innerMethod2 ].
So when the done flag is set, innerMethod1 returns and thereby breaks out of its while loop. Typically code can be re-factored so that this can be done. In your case, you have a multi-way conditional on string prefixes. This can be written:
d _ Dictionary new at: 'ice:' put: [ :s | (s findTokens: 'ice:') first ]; at: ':ELEVATION:' put: [ :s | params at: #elevation put: ((s findTokens: ':ELEVATION:') first)
On Tuesday 29 May 2001 19:14, I wrote:
So when the done flag is set, innerMethod1 returns and thereby breaks out of its while loop. Typically code can be re-factored so that this can be done. In your case, you have a multi-way conditional on string prefixes. This can be written:
d _ Dictionary new at: 'ice:' put: [ :s | (s findTokens: 'ice:') first ]; at: ':ELEVATION:' put: [ :s | params at: #elevation put: ((s findTokens: ':ELEVATION:') first)
Sorry, that was a premature send (I did the Squeak ctrl-Return, and my mailer sent the partial message).
Anyway, what I was going to show is that the iteration of coll could be modeled with a collect: that in some cases might collect nils. This could be followed by a reject: to get rid of the nils.
And this would get rid of all the 'at: i' stuff in the sample (this isn't a very Smalltalky way to do things).
The dictionary I was building above was an attempt to compress the source code.
On Tue, 29 May 2001 reic0024@d.umn.edu wrote:
Hello everyone,
Short question: Does Smalltalk have a way to break out of a block like "break" does in C?
You can return from the block to the calling method ("^self") or you can raise an exception and catch it in an outer block.
-- Bert
squeak-dev@lists.squeakfoundation.org