R/Orify.R

#' Predicate function operator that creates new predicate functions linked by the || operator.
#'
#' @param ... n functions to apply in order from left to right
#' @return A predicate function linked by the || operator.
#' @family predicate function operators
#' @seealso \code{\link{Andify}} to create new predicate functions linked by the && operator.
#' @examples
#' # Examples
#' is_character_or_factor <- Orify(is.character, is.factor)
#' is_character_or_factor(letters) # TRUE
#' is_character_or_factor(factor(state.abb)) # TRUE
#' is_character_or_factor(1:100) # FALSE
#' @export
Orify <- function(...) {
  fs <- lapply(list(...), match.fun)
  first <- fs[[1]]
  rest <- fs[-1]

  function(...) {
    out <- first(...)
    for (f in rest) {
      if (out) return(TRUE)
      out <- `||`(out, f(...))
    }
    out
  }
}

Try the functools package in your browser

Any scripts or data that you put into this service are public.

functools documentation built on May 2, 2019, 5:39 a.m.