Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: Switch with refinements/hmmm!

From: joel:neely:fedex at: 31-Oct-2001 21:01

Hi, Tim and all... Just to recap the interesting variety of responses, there are multiple design decisions to be addressed, depending on the minimum and maximum number of switches/refinements that may be set: 0) Is none an option? If so, is there to be a default behavior? If not, what (if any) error checking/handling is to be performed if none is set? 1) Is one the only option? If so, what (if any) error checking/handling is to be performed if multiple refinements are set? n) Is more than one an option? If not (... error etc. as above...)? If so which action(s) is/are to be performed? The first one selected in textual order? All that are selected? Exactly one, randomly chosen? Nenad Rakocevic wrote:
> If you need to set several refinement at the same time, here's my variant : > > refine-switch: func [/ref-one /ref-two /ref-three][ > all [ref-one print "ref-one"] > all [ref-two print "ref-two"] > all [ref-three print "ref-three"] > ] >
Nice, and very REBOL-flavored, but only works under some circumstances: refine-switch: func [trace [logic!] /refa /refb /refc /local count] [ count: 0 all [refa if trace [print "ref A"] count: count + 1] all [refb if trace [print "ref B"] count: count + 2] all [refc if trace [print "ref C"] count: count + 4] count ] Note the presence of an embedded expression that can evaluate to NONE, which aborts each ALL statement prematurely, as in
>> refine-switch false
== 0
>> refine-switch/refa false
== 0
>> refine-switch/refa true
ref A == 1
>> refine-switch/refa/refb true
ref A ref B == 3
>> refine-switch/refa/refb false
== 0 Using ALL this way depends on all "brances" being made up of expressions that will never return NONE or FALSE. Of course, if that's the case for your particular need, then it'll work just fine. -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" ;