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
yep, i also took a look at that one
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
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 <r.wobben@home.nl> 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 ? or is there a better way ?
Roelof
participants (4)
-
Herby VojÄÃk -
K K Subbu -
Richard O'Keefe -
Roelof Wobben