[ENH] Another new idiom for conditional testing?

danielv at netvision.net.il danielv at netvision.net.il
Tue Oct 2 22:53:32 UTC 2001


Hi Andres.
I have two (pretty tiny) implementations to offer of the pattern I
propose (mental note to self - always remember to cheat by using the
library! it's much more fun ;-)

The simple one, semantically equivalent to yours is -

allTrue 
	^self allSatisfy: [e value]

The usage pattern is as before:

{[exp1]. 
[exp2]. 
[exp3]} allTrue

The second, smarter one is a bit overly complex, but seems quite
convinient -

allTrue 
	^self allSatisfy: [:e | e isBoolean ifTrue: [e] ifFalse: [e value]]

So you only put in blocks things you need to calculate lazily, as in -
{exp1. 
[exp2]. 
exp3. 
[exp4]} allTrue

This is what I meant by not having the block in the example I gave
below, so I resist your correction! :-)

This allows you to say explicitly "This expression is expensive or
dangerous!", which is often worth saying. And it reduces clutter when
they aren't.

Here's my second alternative, adopting your proposed interface and
spirit of minimalist implementation...

Have a look.

Daniel Vainsencher

Andres Valloud <sqrmax at prodigy.net> wrote:
> This is a multi-part message in MIME format.
> --------------BF1F52D56587CDBA1B428870
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7bit
> 
> Hi.
> 
> [Changed the first line into a block, threw in a pair of parenthesis]
> 
> > = aRenameVariableChange
> > ^([self class = aRenameVariableChange class],
> > [className = aRenameVariableChange changeClassName],
> > [isMeta = aRenameVariableChange isMeta],
> > [oldName = aRenameVariableChange oldName],
> > [newName = aRenameVariableChange newName]) allTrue
> 
> Unary selector?  No kidding!  It just reads "I am equal to xyz if all
> these things are true"!  This is great!!!
> 
> > The resulting pattern is not bad, but the implementation creeps me out a
> > bit... adding messages and subclasses to Boolean and BlockContext is a
> > bit extreme for my taste...
> 
> Making it very simple, and just supporting what we want lets us see
> that:
> 
> * We don't need single blocks to be polymorphic with booleans.
> * We don't need BlockContextComposites to understand ifTrue: ifFalse:
> etc, just allTrue, allFalse, etc.
> 
> So, let's just
> 
> 1. Add #, to BlockContext.
> 2. Object subclass: BooleanCondition.
> 
> There's something extra, a particular approach that would deliver an
> even smaller implementation.  We can cascade the queries to the
> condition, so there's no need to implement ifAllTrue:ifAllFalse:,
> ifAnyTrue:ifAnyFalse: neither the more logically complete
> ifAllTrue:ifAnyTrue:ifAllFalse:, plus all its combinations, negations
> and permutations.
> 
> Here's the tiny tiny implementation, with tests.  It just came out so
> beautifully!
> 
> Andres.




More information about the Squeak-dev mailing list