lazylogic: Logical operators using lazy evaluation

lazylogicR Documentation

Logical operators using lazy evaluation

Description

These functions sequentially evaluate their arguments and return a logical value when sufficient information has been acquired to do so. Hence, lazy_any() will not evaluate any arguments beyond the first TRUE, since there is already at least one TRUE value, so TRUE can be returned. Likewise, lazy_all() will not evaluate beyond the first FALSE it is already clear not all arguments are TRUE, so FALSE is returned.

This enables logical chains in which you can use logical statements earlier in the chain to check whether logical statements further down the chain can be evaluated or would cause errors. If a checking statement evaluates to the terminating condition of the used function, further arguments are not evaluated and hence no error is triggered.

Usage

lazy_any(...)

lazy_all(...)

Arguments

...

Expressions evaluating to single logical values.

Value

A logical value.

Author(s)

Sercan Kahveci

Examples

# The final argument is not evaluated because 
# the function reaches its termination condition 
# (TRUE) before that.
lazy_any(FALSE, TRUE, stop())

# Dealing with problematic NULL, NA, and multi-value inputs
myvecs <- list(a=NULL,b=NA,c=c(1,5,2),d=10)
outcomes <- logical(length(myvecs))
for(i in seq_along(myvecs)){
  outcomes[i] <- lazy_all(!is.null(myvecs[[i]]),
                          !length(myvecs[[i]]) != 1,
                          !is.na(myvecs[[i]]),
                          myvecs[[i]] == 10)
}


Spiritspeak/skMisc documentation built on April 12, 2025, 5:40 a.m.