R/purrr.R

Defines functions map_chr map_dbl map_int map_lgl map_mold map

map = function(.x, .f, ...) {
  if (is.function(.f)) {
    lapply(.x, .f, ...)
  } else {
    lapply(.x, `[[`, .f, ...)
  }
}

map_mold = function(.x, .f, .value, ...) {
  out = if (is.function(.f)) {
    vapply(.x, .f, FUN.VALUE = .value, USE.NAMES = FALSE, ...)
  } else {
    vapply(.x, `[[`, .f, FUN.VALUE = .value, USE.NAMES = FALSE, ...)
  }
  stats::setNames(out, names(.x))
}

map_lgl = function(.x, .f, ...) {
  map_mold(.x, .f, NA, ...)
}

map_int = function(.x, .f, ...) {
  map_mold(.x, .f, NA_integer_, ...)
}

map_dbl = function(.x, .f, ...) {
  map_mold(.x, .f, NA_real_, ...)
}

map_chr = function(.x, .f, ...) {
  map_mold(.x, .f, NA_character_, ...)
}

Try the bbk package in your browser

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

bbk documentation built on Aug. 28, 2026, 5:07 p.m.