From 7dc8e7a52ab741ee4df11e651f67bad228933df2 Mon Sep 17 00:00:00 2001 From: Maximilian Muecke Date: Fri, 24 Jan 2025 11:52:19 +0100 Subject: [PATCH 1/2] feat: add compact --- NAMESPACE | 1 + R/purrr_map.R | 6 ++++++ man/compat-map.Rd | 1 + tests/testthat/test_map.R | 9 +++++++++ 4 files changed, 17 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index e45538e2..9fae27b2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -60,6 +60,7 @@ export(chunk_vector) export(cite_bib) export(clbk) export(clbks) +export(compact) export(compose) export(compute_mode) export(count_missing) diff --git a/R/purrr_map.R b/R/purrr_map.R index 9f61d94b..16b84877 100644 --- a/R/purrr_map.R +++ b/R/purrr_map.R @@ -13,6 +13,7 @@ #' * `map_at()` applies `.f` to each element of `.x` referenced by `.at`. All other elements remain unchanged. #' * `keep()` keeps those elements of `.x` where predicate `.p` evaluates to `TRUE`. #' * `discard()` discards those elements of `.x` where predicate `.p` evaluates to `TRUE`. +#' * `compact()` discards elements of `.x` that are `NULL`. #' * `every()` is `TRUE` if predicate `.p` evaluates to `TRUE` for each `.x`. #' * `some()` is `TRUE` if predicate `.p` evaluates to `TRUE` for at least one `.x`. #' * `detect()` returns the first element where predicate `.p` evaluates to `TRUE`. @@ -272,6 +273,11 @@ discard.data.table = function(.x, .p, ...) { # nolint .x[, is.na(.sel) | !.sel, with = FALSE] } +#' @export +compact = function(.x) { # nolint + .x[as.logical(lengths(.x))] +} + #' @export #' @rdname compat-map map_if = function(.x, .p, .f, ...) { diff --git a/man/compat-map.Rd b/man/compat-map.Rd index 38dee256..ca28e082 100644 --- a/man/compat-map.Rd +++ b/man/compat-map.Rd @@ -140,6 +140,7 @@ each vector of \code{.x}, then the second element of \code{.x}, and so on. \item \code{map_at()} applies \code{.f} to each element of \code{.x} referenced by \code{.at}. All other elements remain unchanged. \item \code{keep()} keeps those elements of \code{.x} where predicate \code{.p} evaluates to \code{TRUE}. \item \code{discard()} discards those elements of \code{.x} where predicate \code{.p} evaluates to \code{TRUE}. +\item \code{compact()} discards elements of \code{.x} that are \code{NULL}. \item \code{every()} is \code{TRUE} if predicate \code{.p} evaluates to \code{TRUE} for each \code{.x}. \item \code{some()} is \code{TRUE} if predicate \code{.p} evaluates to \code{TRUE} for at least one \code{.x}. \item \code{detect()} returns the first element where predicate \code{.p} evaluates to \code{TRUE}. diff --git a/tests/testthat/test_map.R b/tests/testthat/test_map.R index 1e2f0b0e..7ce457a3 100644 --- a/tests/testthat/test_map.R +++ b/tests/testthat/test_map.R @@ -201,6 +201,15 @@ test_that("detect", { expect_null(out) }) +test_that("compact", { + x = list(a = 1:3, b = c("a", "b"), c = NULL, d = NULL) + out = compact(x) + expect_identical(out, x[1:2]) + x = list(a = 1:3, b = c("a", "b"), c = runif(3)) + out = compact(x) + expect_identical(out, x) +}) + test_that("pmap does not segfault (#56)", { expect_error(pmap(1:4, function(x) x), "list") }) From 30d9f529ba4d5308f6669e099d2cccc4f292f054 Mon Sep 17 00:00:00 2001 From: Maximilian Muecke Date: Fri, 24 Jan 2025 12:04:07 +0100 Subject: [PATCH 2/2] docs: missing document --- R/purrr_map.R | 1 + man/compat-map.Rd | 3 +++ 2 files changed, 4 insertions(+) diff --git a/R/purrr_map.R b/R/purrr_map.R index 16b84877..71acd540 100644 --- a/R/purrr_map.R +++ b/R/purrr_map.R @@ -274,6 +274,7 @@ discard.data.table = function(.x, .p, ...) { # nolint } #' @export +#' @rdname compat-map compact = function(.x) { # nolint .x[as.logical(lengths(.x))] } diff --git a/man/compat-map.Rd b/man/compat-map.Rd index ca28e082..020aabed 100644 --- a/man/compat-map.Rd +++ b/man/compat-map.Rd @@ -27,6 +27,7 @@ \alias{imap_dtc} \alias{keep} \alias{discard} +\alias{compact} \alias{map_if} \alias{map_if.default} \alias{map_at} @@ -88,6 +89,8 @@ keep(.x, .f, ...) discard(.x, .p, ...) +compact(.x) + map_if(.x, .p, .f, ...) \method{map_if}{default}(.x, .p, .f, ...)