lazylogic | R Documentation |
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.
lazy_any(...)
lazy_all(...)
... |
Expressions evaluating to single logical values. |
A logical value.
Sercan Kahveci
# 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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.