[REBOL] Re: Switch with refinements/hmmm!
From: joel:neely:fedex at: 1-Nov-2001 19:44
Hi, Romano,
Yet another interesting specimen for our growing menagerie!
Romano Paolo Tenca wrote:
> Hi, Joel Neely
>
> > reftree: func [/refa /refb /refc] [
> > either refa
> > [either refb
> > [either refc
> > [print "all refs are set!"]
> > [print "ref A & B"]]
...
> > ]
> >
> > (which only evaluates each refinement once) it looks like a classic
> > case of "pay me now or pay me later". We either spend programmer
> > time to craft a faster process or we spend the cycles at run time.
>
> Why not:
>
> v: to-string replace/all reduce [ all[refa "a"] all [refb "b"] all [refc "c"]]
> none ""
> print select ["" "no refs" "a" "refa" "b" "refb" "ab" "refa & b" "c" "refc"
> "ac" "refa & refc" "bc" "refb & refc" "abc" "refa & refb & refc"] v
>
Well (first putting it into a function for apples-to-apples comparison, and
eliminating the superfluous variable ...) it seems clear to me that we have
yet another tradeoff in this version:
ref-sw: func [/refa /refb /refc] [
print select [
"" "no refs"
"a" "ref A"
"b" "ref B"
"ab" "ref A & B"
"c" "ref C"
"ac" "ref A & C"
"bc" "ref B & C"
"abc" "all refs"
] to-string replace/all reduce [
all [refa "a"]
all [refb "b"]
all [refc "c"]
] none ""
]
which certainly evaluates each refinement exactly once. Given that the
number of combinations in the "lookup table" is exponential with the
number of refinements, there's also clearly *some* point where it becomes
slower than the explicit decision tree.
Where that point is would clearly be a topic for a bit of benchmarking...
> I find it more rebolesque. It is easy to mantain. It is fast. It is not
> C-like. It can be also more simple, but less readable:
>
> print select [[none none none] "no refs" [true none none] "a"...] reduce [
> refa refb refc ]
>
... and likewise for this combinatorial version.
Thanks for the interesting alternatives!!!
-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" ;