R/compact-purrr.R

Defines functions reduce map2_chr map2 map_chr map_dbl map_mold map

# nocov start

# This file serves as a reference for compatibility functions for
# purrr. They are not drop-in replacements but allow a similar style
# of programming. This is useful in cases where purrr is too heavy a
# package to depend on.

map <- function(.x, .f, ...) {
  lapply(.x, .f, ...)
}

map_mold <- function(...) {
  out <- vapply(..., USE.NAMES = FALSE)
  names(out) <- names(..1)
  out
}

map_dbl <- function(.x, .f, ...) {
  map_mold(.x, .f, double(1), ...)
}

map_chr <- function(.x, .f, ...) {
  map_mold(.x, .f, character(1), ...)
}

map2 <- function(.x, .y, .f, ...) {
  mapply(.f, .x, .y, MoreArgs = list(...), SIMPLIFY = FALSE)
}

map2_chr <- function(.x, .y, .f, ...) {
  as.vector(map2(.x, .y, .f, ...), "character")
}

reduce <- function(.x, .f, ..., .init) {
  f <- function(x, y) .f(x, y, ...)
  Reduce(f, .x, init = .init)
}

# nocov end
mitchelloharawild/fable.prophet documentation built on Oct. 17, 2023, 11:22 a.m.