R/adverb-negate.R

Defines functions negate

Documented in negate

#' Negate a predicate function so it selects what it previously rejected
#'
#' Negating a function changes `TRUE` to `FALSE` and `FALSE` to `TRUE`.
#'
#' @inheritParams keep
#' @inheritSection safely Adverbs
#' @family adverbs
#' @return A new predicate function.
#' @export
#' @examples
#' x <- list(x = 1:10, y = rbernoulli(10), z = letters)
#' x |> keep(is.numeric) |> names()
#' x |> keep(negate(is.numeric)) |> names()
#' # Same as
#' x |> discard(is.numeric)
negate <- function(.p) {
  compose(`!`, as_mapper(.p))
}

Try the purrr package in your browser

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

purrr documentation built on Aug. 10, 2023, 9:08 a.m.