[newbie] Complex boolean value?
Richard A. O'Keefe
ok at atlas.otago.ac.nz
Sun Jun 17 23:38:01 UTC 2001
Sorry about my stupid question but I started to experiment with Squeak
yesterday and when I wanted to come up with Boolean expression to check
if any of the 3 conditions is true, I came up with this:
((value < 1 or: [value > x1]) or: [value > x2])
ifTrue: [self error: 'Invalid value'].
(1) Rearrange like this:
(value < 1 or: [value > x1 or: [value > x2]])
ifTrue: [self error: 'Invalid value'].
(2) v > x OR v > y if and only if v > max(x,y), so
(value < 1 or: [value > (x1 max: x2)])
ifTrue: [self error: 'Invalid value'].
(3) Use #between:and#
(value between: 1 and: (x1 max: x2))
ifFalse: [self error: 'Invalid value'].
(4) The parenthesis count could be reduced still further if min: and max:
were operators instead of keywords. So stick
/\ x ^self min: x
\/ x ^self max: x
in Magnitude, and you can write
(value between: 1 and: x1 \/ x2)
ifFalse: [self error: 'Invalid value'].
I rather like /\ and \/, having once used a C compiler that supported them.
(Mind you, I had to fix a few bugs in the code generator before I could
actually _use_ these operators...) However, since #min: and #max: already
exist, (4) would make the code *less* readable by other Smalltalkers, so I
don't actually recommend doing it.
If I were you, I'd go with (3).
More information about the Squeak-dev
mailing list
|