Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export(chunk_vector)
export(cite_bib)
export(clbk)
export(clbks)
export(compact)
export(compose)
export(compute_mode)
export(count_missing)
Expand Down
7 changes: 7 additions & 0 deletions R/purrr_map.R
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -272,6 +273,12 @@ discard.data.table = function(.x, .p, ...) { # nolint
.x[, is.na(.sel) | !.sel, with = FALSE]
}

#' @export
#' @rdname compat-map
compact = function(.x) { # nolint
.x[as.logical(lengths(.x))]
}

#' @export
#' @rdname compat-map
map_if = function(.x, .p, .f, ...) {
Expand Down
4 changes: 4 additions & 0 deletions man/compat-map.Rd

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

9 changes: 9 additions & 0 deletions tests/testthat/test_map.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Expand Down
Loading