R/utils.R

Defines functions detect_value index detect_index detect

detect <- function(.x, .f, ..., .right = FALSE, .p = is_true) {
  for (i in index(.x, .right)) {
    if (.p(.f(.x[[i]], ...))) {
      return(.x[[i]])
    }
  }
  NULL
}
detect_index <- function(.x, .f, ..., .right = FALSE, .p = is_true) {
  for (i in index(.x, .right)) {
    if (.p(.f(.x[[i]], ...))) {
      return(i)
    }
  }
  0L
}
index <- function(x, right = FALSE) {
  idx <- seq_along(x)
  if (right) {
    idx <- rev(idx)
  }
  idx
}

detect_value <- function(.x, .f, .p, ..., .right = FALSE) {
  for (i in index(.x, .right)) {
    value <- .f(.x[[i]], ...)
    if (.p(value)) {
      return(value)
    }
  }
  NULL
}
lionel-/redpen documentation built on Jan. 12, 2018, 2:11 p.m.