[REBOL] Re: Switch with refinements/hmmm!
From: joel:neely:fedex at: 2-Nov-2001 7:08
Hi, again,
Media wrote:
> Joel, the advantage of supplying each condition is that you can then trap
> any COMBINATION by itself... not just if a value is set by itself...
>
> But once its on "paper" I find that any/all combos are much easier to
> maintain, simply because the expressions themselves are easier to extract
> visually...
>
> any [
> all [refa refb refc print "condition a & b & c met"]
> all [refa refb print "condition a & b met"]
> all [refc]
> print "nothing set"
> ]
>
> But as you said, its just a question of fast to maintain vs fast to
> execute vs fast to implement...
>
That's *one* of the issues; the other is that the ALL trick shorts-out
if one of the subsequent expressions (subsequent to the things actually
being tested, that is) evaluates to FALSE or NONE. I firmly believe
that ALL is only appropriate if *every* expression's value should be
part of the continue/quit decision. When that is not the case, it's
easy to rewrite to signal which expressions are and are not part of that
decision, as follows:
any [
if all [refa refb refc] [print "A and B and C"]
if all [refa refb ] [print "A and B" ]
if all [refc ] [ ]
print "nothing set"
]
This variant has all the nice properties of compactness, visual separation,
maintainability, etc. of the ANY/ALL version, but also avoids confusion (or
induced bugs) over the issue of which expression values are tested.
As I mentioned previously, a discriminating scenario for this issue is
any [
if all [refa refb refc] [no-a: false print "A B C" ]
if all [refa refb ] [no-a: false print "A B" ]
if all [refc ] [no-a: true print "C" ]
no-a: true print "nothing"]
]
which would bail out prematurely (and erroneously) in the simple ANY/ALL
pattern.
-jn-
> ----- Original Message -----
> From: "Romano Paolo Tenca" <[rotenca--telvia--it]>
> To: <[rebol-list--rebol--com]>
> Sent: Thursday, November 01, 2001 7:20 PM
> Subject: [REBOL] Re: Switch with refinements/hmmm!
>
...
> >
> > print select [[none none none] "no refs" [true none none] "a"...] reduce [
> > refa refb refc ]
> >
The last version above is broken. It falls prey to the "literal words" issue
we've discussed several times regarding SELECT, since NONE and TRUE will
actually be 'NONE and 'TRUE in the above unless reduction is done. In addition,
the SELECT will fail unless the /ONLY refinement is used, since we're trying to
use a block as the "selection key". The simplest fix I've come up with so far
is
print select/only reduce [
reduce [none none none] "no refs"
reduce [true none none] "ref a"
...
] reduce [refa refb refc]
Incidentally, I'm working up a set of benchmarks on all of the variations
discussed in this thread; I'll publish results later today.
-jn-
--
; sub REBOL {}; sub head ($) {@_[0]}
REBOL []
# despam: func [e] [replace replace/all e ":" "." "#" "@"]
; sub despam {my ($e) = @_; $e =~ tr/:#/.@/; return "\n$e"}
print head reverse despam "moc:xedef#yleen:leoj" ;