Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes list_modify to be able to remove key-value pairs whose value is a list #777

Merged
merged 3 commits into from
Jul 31, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

# purrr 0.3.4

* Fixed issue in `list_modify()` that prevented lists from being
removed with `zap()` (@adamroyjones, #777).

* Added documentation for exporting functions created with purrr
adverb (@njtierney, #668). See `?faq-adverbs-export`.

Expand Down
5 changes: 3 additions & 2 deletions R/list-modify.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ list_recurse <- function(x, y, base_case) {
abort("`...` arguments must be either all named, or all unnamed")
}

# N.B. is_list(zap()) is TRUE.
if (is_null(y_names)) {
for (i in rev(seq_along(y))) {
if (i <= length(x) && is_list(x[[i]]) && is_list(y[[i]])) {
if (i <= length(x) && is_list(x[[i]]) && is_list(y[[i]]) && !is_zap(y[[i]])) {
x[[i]] <- list_recurse(x[[i]], y[[i]], base_case)
} else {
x[[i]] <- maybe_zap(base_case(x[[i]], y[[i]]))
Expand All @@ -80,7 +81,7 @@ list_recurse <- function(x, y, base_case) {
} else {
for (i in seq_along(y_names)) {
nm <- y_names[[i]]
if (has_name(x, nm) && is_list(x[[nm]]) && is_list(y[[i]])) {
if (has_name(x, nm) && is_list(x[[nm]]) && is_list(y[[i]]) && !is_zap(y[[i]])) {
x[[nm]] <- list_recurse(x[[nm]], y[[i]], base_case)
} else {
x[[nm]] <- maybe_zap(base_case(x[[nm]], y[[i]]))
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-list-modify-update.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ test_that("unnamed lists are replaced by position", {
test_that("can remove elements with `zap()`", {
expect_equal(list_modify(list(1, 2, 3), zap(), zap()), list(3))
expect_equal(list_modify(list(a = 1, b = 2, c = 3), b = zap(), a = zap()), list(c = 3))
expect_equal(
list_modify(list(a = list(fst = 1, snd = 2), b = 2, c = 3), b = zap(), a = zap()),
list(c = 3)
)
})

test_that("error if inputs are not all named or unnamed", {
Expand Down