[REBOL] el(se)if constriction
From: peoyli:algonet:se at: 27-Oct-2000 16:21
Here's a function I wrote that I find useable (or, is there a better
way of doing this ?):
elif: func [
{Do the block that a test condition is true for}
elif-block [block!] {A block of conditions and blocks to be evaluated on a match}
/default def_case {A block to evaluate if no conditions were true}
][
either found? find reduce elif-block true [
do second find reduce elif-block true
][
either default [
do def_case
][
none
]
]
]
-- Attached file included as plaintext by Listar --
-- File: elif-examples.r
REBOL []
; Example 1: a simple match, check one word for an exact value (which
; also could be done with the switch function
; Result: "third match"
;
line1: "third match"
elif [
line1 = "first match" [
print "first match"
]
line1 = "second match" [
print "second match"
]
line1 = "third match" [
print "third match"
]
line1 = "fourth match" [
print "fourth match"
]
]
; Example 2: check two words for the first value that matches
; Result: "just another test"
;
line1: "nothing"
line2: "just another test"
elif [
line1 = "first match" [
print "first match"
]
line1 = "second match" [
print "second match"
]
line1 = "third match" [
print "third match"
]
line2 = "just another test" [
print "just another test"
]
line1 = "fourth match" [
print "fourth match"
]
line1 = "nothing" [
print "should never get here"
]
]
; Example 3: check two words, but also use a default for no match
; Result: "no match"
;
line1: "nothing"
line2: "some more tests"
elif/default [
line1 = "first match" [
print "first match"
]
line1 = "second match" [
print "second match"
]
line1 = "third match" [
print "third match"
]
line1 = "fourth match" [
print "fourth match"
]
line2 = "just another test" [
print "just another test"
]
][
print "no match"
]
; Example 4: check two words, which the second of them is checked with
; the parse function. Supply a default if nothing matches
; Result: "more"
;
line1: "nothing"
line2: "some more tests"
elif/default [
line1 = "first match" [
print "first match"
]
line1 = "second match" [
print "second match"
]
line1 = "third match" [
print "third match"
]
line1 = "fourth match" [
print "fourth match"
]
parse line2 [to "more" copy result to "tests" to end] [
print result
]
line2 = "just another test" [
print "just another test"
]
][
print "no match"
]