Mailing List Archive: 49091 messages

## [REBOL] Re: If without a THEN

### From: joel:neely:fedex at: 9-Aug-2001 17:31

```
Hi, Ryan et al.,

Ryan Cole wrote:
> Looked weird to me too, it never occurred to me to use 'if
> that way.
>
> >> something: true
> == true
> >> reaction: [print "HOLY COW!"]
> >> if something reaction
> HOLY COW!
> >>
>

An equivalent concept has been common practice in FORTH for
at least 15-20 years as a way to refactor decision logic,
such as moving always-the-same decisions out of a loop.

As a trivial example, suppose we need to take a block of numbers
and calculate either the square of each number or the square root
of each number (where the decision is the same for all number in
a single evaluation of the function).  We could write:

square-or-root: func [flag [logic!] b [block!] /local c] [
c: copy b
forall c [
either flag [
change c (first c) * first c
][
change c square-root first c
]
]
]

print mold square-or-root true  [1 4 9 16]
print mold square-or-root false [1 4 9 16]

which behaves as follows:

>> print mold square-or-root true  [1 4 9 16]
[1 16 81 256]
>> print mold square-or-root false [1 4 9 16]
[1 2 3 4]

The politically-correct but trivial refactoring into

square-or-root: func [flag [logic!] b [block!] /local c] [
c: copy b
forall c [
change c either flag [
(first c) * first c
][
c square-root first c
]
]
]

gives the same result, and still leaves the decision inside the
loop, to be made over and over the same way each time.  However,

square-or-root: func [flag [logic!] b [block!] /local c work] [
c: copy b
work: either flag [
[change c(first c) * first c]
][
[change c square-root first c]
]
forall c work
]

takes the decision outside the loop.  Now the block in WORK is
pre-configured
to do The Right Thing based on the FLAG argument
that doesn't change during the evaluations of the loop.

HTH!

-jn-

This sentence contradicts itself -- no actually it doesn't.