R/dfs_idx.R

Defines functions dfs_idx

Documented in dfs_idx

#' Perform a recursive depth first search of a function
#' @inheritParams purrr::map
#' @export
dfs_idx <- function(.x, .f) {
  .f <- purrr::as_mapper(.f)
  res <- list()
  num <- 0L
  walk <- function(x, idx) {
    for (i in seq_along(x)) {
      if (isTRUE(tryCatch(.f(x[[i]]), error = function(e) FALSE))) {
        res[[num <<- num + 1L]] <<- append(idx, i)
      }
      if (is.list(x[[i]])) {
        walk(x[[i]], append(idx, i))
      }
    }
  }
  walk(.x, integer())
  res
}
ropenscilabs/roomba documentation built on July 26, 2021, 7:37 p.m.