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

[REBOL] Re: a new switch-like flow-control function

From: joel:neely:fedex at: 17-Dec-2003 10:19

Hi, Anton, Set the WayBack machine to 2000... ;-) Anton Rolls wrote:
> I yearn (want), occasionally, a kind of any/switch-like > flow-control function...
Some years ago we had a long discussion about generalizing IF on the list, and kicked around various options/versions. The copy I could lay my hands on most quickly was Ladislav's refinement (the last in the discussion???): pif: func [[throw catch] args [block!] /local res] [ either unset? first res: do/next args [ if not empty? args [ throw make error! [script no-arg pif condition] ] ][ either first res [ either block? first res: do/next second res [ do first res ][ throw make error! [ script expect-arg pif block [block!] ] ] ][ pif second do/next second res ] ] ] PIF (Polymorphic IF) takes a block of guard-expression/block pairs and uses the (first true) guard-expressions to select block to be evaluated. It can be used (I believe) essentially as you were asking. Refactoring PIF to iterative form is left as an exercise for the reader! ;-) As long as the argument block isn't too long the recursion likely won't be a problem. Heres a sample usage: describe: func [x [integer!] y [integer!]] [ pif [ x = 1 ["X is one"] y = 1 ["Y is one"] x = 2 ["X is two"] y = 2 ["Y is two"] true ["X and Y are too big!"] ] ] which behaves as:
>> describe 1 1
== "X is one"
>> describe 2 1
== "Y is one"
>> describe 2 2
== "X is two"
>> describe 3 2
== "Y is two"
>> describe 3 3
== "X and Y are too big!" -jn- -- ---------------------------------------------------------------------- Joel Neely joelDOTneelyATfedexDOTcom 901-263-4446 Enron Accountingg in a Nutshell: 1c=$0.01=($0.10)**2=(10c)**2=100c=$1