R/modifiers.R

Defines functions not or and assert

Documented in assert

assert <- function(p) {
  predicate_name <- deparse(
    substitute(p)
  )

  function(x) {
    object_name <- deparse(
      substitute(x)
    )
    error_message <- paste0(
      "Assertion failed. The predicate ",
      "'", predicate_name, "'",
      " returned FALSE with input ",
      "'", object_name, "'", "."
    )
    if (isTRUE(p(x))) {
      invisible(x)
    } else {
      stop(
        error_message,
        call. = FALSE
      )
    }
  }
}

and <- function(...) {
  function(x) {
    reduce(
      fn = function(acc, p) {
        acc && p(x)
      },
      init = TRUE
    )(x = list(...))
  }
}

or <- function(...) {
  function(x) {
    reduce(
      fn = function(acc, p) {
        acc || p(x)
      },
      init = FALSE
    )(x = list(...))
  }
}

not <- function(p) {
  function(x) {
    !p(x)
  }
}
armcn/pure documentation built on Dec. 30, 2021, 12:16 a.m.