REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 17-Aug-2010 Edit History  

REBOL 3 Functions: any

any  block

Shortcut OR. Evaluates and returns the first value that is not FALSE or NONE.

Arguments:

block [block!] - Block of expressions

See also:

all   or   and   case   switch  

Description

The any function is the most common way to test for one of multiple conditions, such as in the line:

if any [a > 10  b > 20  c > 30] [do something]

Here, if any one of the conditions produces a true result, the if will evaluate the block.

This function works by evaluating each expression in a block until one of the expressions returns a value other than none! or false, in which case the value is returned. Otherwise, none! will be returned.

Examples to help show how it works:

print any [1 none]
1
print any [none 1]
1
print any [none none]
none
print any [false none]
none
print any [true none]
true
time: 10:30
if any [time > 10:00  time < 11:00] [print "time is now"]
time is now

No other expressions are evaluated beyond the point where a successful value is found. This can be useful. For example:

a: 0
any [none a: 2]
print a
2
a: 0
any [1 a: 2]
print a
0
day: 10
time: 9:45
ready: any [day > 5  time < 10:00  time: 12:00]
print time
9:45

The any function is also useful for setting default values. For example:

size: any [size 100]

If size was none!, then it gets set to 100. This works even better if there are alternative defaults:

size: any [size prefs/size 100]

Another use for any is to emulate a sequence of if...elseif...elseif...else. Instead of writing:

either cond-1 [
    code-1
] [
    either cond-2 [
        code-2
    ] [
        either cond-3 ...
    ]
]

it is possible to write:

any [
    if cond-1 [
        code-1
        true ; in case code-1 returns FALSE or NONE
    ]
    if cond-2 [
        code-2
        true
    ]
    ...
]

Also see the case function for more about this code pattern.

The all function is a companion of any to test for the opposite condition, where all of the values must be true to return a true result.


  TOC < Back Next > REBOL.com - WIP Wiki Feedback Admin