Skip to content

Commit

Permalink
Replace csvwr::vec_depth with purrr::pluck_depth
Browse files Browse the repository at this point in the history
  • Loading branch information
Robsteranium committed Jan 21, 2024
1 parent c4b6264 commit 7073e9f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 47 deletions.
32 changes: 9 additions & 23 deletions R/util.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
rmap <- function(.x, .f) {
m <- function(le) {
v <- le[[1]]
r <- if(purrr::vec_depth(v)==1) {
r <- if(purrr::pluck_depth(v)==1) {
.f(v)
} else {
purrr::lmap(v, m)
Expand All @@ -22,34 +22,20 @@ rmap <- function(.x, .f) {
purrr::lmap(.x, m)
}

#' Calculate depth of vector safely

#' Identify csvw properties
#'
#' Like `purrr::vec_depth` but doesn't attempt to descend into errors
#' Overrides the default behaviour in `purrr::pluck_depth` so that we don't descend into errors
#'
#' @param x a vector
#' @return An integer
#' @return A boolean
#' @md
#' @keywords internal
vec_depth <- function (x)
{
if (rlang::is_null(x)) {
0L
}
else if (rlang::is_atomic(x)) {
1L
}
else if (inherits(x, "error")) {
1L
}
else if (rlang::is_list(x)) {
depths <- purrr::map_int(x, vec_depth)
1L + max(depths, 0L)
}
else {
rlang::abort("`x` must be a vector")
}
is_property <- function(x) {
(is.expression(x) || is.list(x)) & !inherits(x, "error")
}


#' Recursive lmap
#'
#' Applies function `.f` to each list-element in `.x` as per `purrr::lmap`.
Expand All @@ -64,7 +50,7 @@ vec_depth <- function (x)
#' @keywords internal
rlmap <- function(.x, .f, ...) {
m <- function(le) {
r <- if(vec_depth(le[[1]])<=1) {
r <- if(purrr::pluck_depth(le[[1]], is_node=is_property)<=1) {
.f(le, ...)[[1]]
} else {
purrr::lmap(le[[1]], m)
Expand Down
18 changes: 18 additions & 0 deletions man/is_property.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 0 additions & 18 deletions man/vec_depth.Rd

This file was deleted.

6 changes: 0 additions & 6 deletions tests/testthat/test-util.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,3 @@ test_that("rlmap doesn't attempt to map into errors", {
test_that("rlmap returns NULL unaltered", {
expect_null(rlmap(list(foo=NULL), function(x) x)$foo)
})

test_that("vec_depth can measure the depth of errors", {
err <- tryCatch(stop("oops"), error=function(e) {e})
expect_equal(vec_depth(err), 1)
})

0 comments on commit 7073e9f

Please sign in to comment.