Hello,
For a challenge of Exercism I need to check if some parenthes and brackets are balanced. so for example () = true ([]) = true but (]) is not true because the bracket has no opening bracket. Now I wonder if I can premature end a #do: like this collection do: [:element | (condition with element) ifFalse: [^false]] in pseudo code is this a valid way to end the do ? or is there a better way ? Roelof |
On 04/04/19 4:46 PM, Roelof Wobben wrote:
> Hello, > > For a challenge of Exercism I need to check if some parenthes and > brackets are balanced. > > so for example > > () = true > ([]) = true > > but (]) is not true because the bracket has no opening bracket. > > Now I wonder if I can premature end a #do: like this > > collection do: [:element | (condition with element) ifFalse: [^false]] Have you looked at anySatisfy: #('the' 'quick' 'brown' 'fox') anySatisfy: [:s | s beginsWith: 'x' ] Regards .. Subbu |
Op 4-4-2019 om 13:22 schreef K K Subbu:
> On 04/04/19 4:46 PM, Roelof Wobben wrote: >> Hello, >> >> For a challenge of Exercism I need to check if some parenthes and >> brackets are balanced. >> >> so for example >> >> () = true >> ([]) = true >> >> but (]) is not true because the bracket has no opening bracket. >> >> Now I wonder if I can premature end a #do: like this >> >> collection do: [:element | (condition with element) ifFalse: [^false]] > > Have you looked at anySatisfy: > > #('the' 'quick' 'brown' 'fox') anySatisfy: [:s | s beginsWith: 'x' ] > > Regards .. Subbu > |
In reply to this post by Roelof
On 4. 4. 2019 13:16, Roelof Wobben wrote:
> Hello, > > For a challenge of Exercism I need to check if some parenthes and > brackets are balanced. > > so for example > > () = true > ([]) = true > > but (]) is not true because the bracket has no opening bracket. > > Now I wonder if I can premature end a #do: like this > > collection do: [:element | (condition with element) ifFalse: [^false]] > > in pseudo code > > is this a valid way to end the do ? Not sure I understand your question properly, but yes, you can do this, it is called "non-local return" and is a known pattern in Smalltalk. > or is there a better way ? (collection detect: [:element | (condition with element) not] ifNone: [sentinel]) == sentinel ifFalse: [ ^false ] for example does not force you to employ it, as well as (collection allSatisfy: [:element | (condition with element)]) ifFalse: [ ^false ] > > Roelof Herby |
In reply to this post by Roelof
For a change this is an exercism problem I know something about. I did look at the exercism web site, and did all the SML exercises, but could not find any for Smalltalk or Pharo. The exercise is in fact about using a stack. stack <- empty for each character of the string if it is one of ( [ { push it on the stack if it is one of ) ] } if the stack is empty or the top element does not correspond to this character, return false if it is not one of ( ) [ ] { } just ignore it return (the stack is empty) BOTH of the "return" constructs in this pseudo-code become ^ something in Smalltalk. "Long returns", where "^" occurs inside a block, are well defined, very useful, and universally accepted in Smalltalk. There is no analogue of "^" for returning from a block. By the way, this has nothing to do with #do:. It's about returning from the method through any number of blocks. For example, you will sometimes see code like x := aDictionary at: aKey ifAbsent: [^false]. On Fri, 5 Apr 2019 at 00:16, Roelof Wobben <[hidden email]> wrote: Hello, |
I already thought to use this flow.
but I only wondered if I could use the ^false this way and appearently it is allowed. so I can begin with this one. The Pharo track is still hidden but I saw on the github page that in a short time it will be public with some 20 - 25 challenges. Roelof Op 4-4-2019 om 14:36 schreef Richard O'Keefe:
|
Free forum by Nabble | Edit this page |