Rebol3 Code Examplex
Short-circuit evaluation
Rebol [
title: "Rosetta code: Short-circuit evaluation"
file: %Short-circuit_evaluation.r3
url: https://rosettacode.org/wiki/Short-circuit_evaluation
]
a: func [v][ print ["called function A with:" v] v]
b: func [v][ print ["called function B with:" v] v]
print as-yellow "Using AND/OR:"
print as-gray "AND/OR always evaluate both arguments — no short-circuit"
print ""
foreach i [#(true) #(false)] [
foreach j [#(true) #(false)] [
print [tab "The result of A(i) AND B(j) is:" (a i) and (b j)]
]
]
print ""
foreach i [#(true) #(false)] [
foreach j [#(true) #(false)] [
print [tab "The result of A(i) OR B(j) is:" (a i) or (b j)]
]
]
print-hline/width 50
print as-yellow "Using ALL/ANY:"
print as-gray "ALL/ANY evaluate left to right and stop as soon as the result is determined:"
print as-gray "ALL stops on first false, ANY stops on first true — so B may not be called"
print ""
foreach i [#(true) #(false)] [
foreach j [#(true) #(false)] [
print [tab "The result of ALL [A(i) B(j)] is:" all [(a i) (b j)]]
]
]
print ""
foreach i [#(true) #(false)] [
foreach j [#(true) #(false)] [
print [tab "The result of ANY [A(i) B(j)] is:" any [(a i) (b j)]]
]
]Output:
Using AND/OR:
AND/OR always evaluate both arguments — no short-circuit
called function A with: true
called function B with: true
The result of A(i) AND B(j) is: true
called function A with: true
called function B with: false
The result of A(i) AND B(j) is: false
called function A with: false
called function B with: true
The result of A(i) AND B(j) is: false
called function A with: false
called function B with: false
The result of A(i) AND B(j) is: false
called function A with: true
called function B with: true
The result of A(i) OR B(j) is: true
called function A with: true
called function B with: false
The result of A(i) OR B(j) is: true
called function A with: false
called function B with: true
The result of A(i) OR B(j) is: true
called function A with: false
called function B with: false
The result of A(i) OR B(j) is: false
--------------------------------------------------
Using ALL/ANY:
ALL/ANY evaluate left to right and stop as soon as the result is determined:
ALL stops on first false, ANY stops on first true — so B may not be called
called function A with: true
called function B with: true
The result of ALL [A(i) B(j)] is: true
called function A with: true
called function B with: false
The result of ALL [A(i) B(j)] is: none
called function A with: false
The result of ALL [A(i) B(j)] is: none
called function A with: false
The result of ALL [A(i) B(j)] is: none
called function A with: true
The result of ANY [A(i) B(j)] is: true
called function A with: true
The result of ANY [A(i) B(j)] is: true
called function A with: false
called function B with: true
The result of ANY [A(i) B(j)] is: true
called function A with: false
called function B with: false
The result of ANY [A(i) B(j)] is: none